Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileCompilerTags not working for field rendering #313

Open
dreerr opened this issue Jul 9, 2017 · 5 comments
Open

FileCompilerTags not working for field rendering #313

dreerr opened this issue Jul 9, 2017 · 5 comments

Comments

@dreerr
Copy link

dreerr commented Jul 9, 2017

Short description of the issue

With the module FileCompilerTags activated it is possible to use {var} or {var.property} variables in markup sections of a file. Sadly this does not work for template files for fields stored in templates/fields/

Expected behavior

When using {var} in a file in templates/fields/ it should render the value.

Actual behavior

The {var} is not parsed.

Steps to reproduce the issue

  1. Create a field template
  2. Use a tag like {value.title}

Setup/Environment

ProcessWire: 3.0.64
PHP: 7.1.4
Webserver: Apache/2.4.18 (Unix)
MySQL: 5.7.17

@ryancramerdesign
Copy link
Member

In your file that's in templates/fields/, are you using a "namespace ProcessWire;" at the top? If so, try removing that, so that there is no namespace specified. Those field rendering files don't get touched by the compiler if a namespace is present.

@dreerr
Copy link
Author

dreerr commented Jul 14, 2017

No, there is no namespace declaration, also not in the corresponding template file.

@netcarver
Copy link
Collaborator

Is this issue impacting anyone other than dreer?

@matjazpotocnik
Copy link
Collaborator

matjazpotocnik commented Feb 17, 2019

I can confirm what @dreerr found out. Possible solution:

$wire->addHookAfter('FileCompiler::compileData', function(HookEvent $event) {
	$data = $event->return;
	$sourceFile = $event->arguments[1];
	
	//only compile files inside fields folder
	if(strpos($sourceFile, '/fields/') === false) return;
	
	$compilers = array();
	foreach($this->wire('modules')->findByPrefix('FileCompiler', true) as $module) {
		if(!$module instanceof FileCompilerModule) continue;
		$runOrder = (int) $module->get('runOrder');
		while(isset($compilers[$runOrder])) $runOrder++;
		$compilers[$runOrder] = $module;
	}

	if(count($compilers)) {
		ksort($compilers);
		foreach($compilers as $module) {
			$module->setSourceFile($sourceFile);
			$data = $module->compile($data);
		}
	}
	
	$event->return = $data;
});

Also, this is what Ryan said in blog:

This particular module is in the core presently, but will likely be moved out of the core once 3.x has representation in the modules directory. But for those interested in implementing their own FileCompiler module(s), we'd encourage you to use the FileCompilerTags module as a potential Hello World example of implementing this module class.

@netcarver
Copy link
Collaborator

@matjazpotocnik Thank you, added labels accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants