multiple template directories

Notice: This thread is very old.
tomcat4x
Member | 22
+
0
-

Hi,

first of all congratulations to this framework.

But i am not sure, if this framework fulfill my requirements.
.
I am looking for a solution to have multiple template sets. All the templates should be stored in one directory with subdirectorties which are named different.

/wwwroot
/templates
/templateset1
/templateset2

Dependend on a parameter (from neon or later from database) nette should chose the corresponding templateset

Is this possible with nette? If yes, whats the best way?

Šaman
Member | 2528
+
+1
-

Templates are finded by formatTemplateFiles() and formatLayoutTemplateFiles() methods in Presenter.
Overwrite that methods in your BasePresenter and change directory as you can.

Last edited by Šaman (2015-07-02 11:51)

tomcat4x
Member | 22
+
0
-

That works fine, if i declare the new template path static.

But how do i get informations from neon or a database in the basePresenter? There will never be an instance of this class. And eg. in the HompagePresenter its to late do declare the template path.

tomcat4x
Member | 22
+
0
-

Here is my solution:

abstract class BasePresenter extends Nette\Application\UI\Presenter
{

	private $templateName ="default";

	public function formatLayoutTemplateFiles()
	{
		$this->setTemplateName();
		$name = $this->getName();
		$presenter = substr($name, strrpos(':' . $name, ':'));
		$layout = $this->layout ? $this->layout : 'layout';
		$dir =$_SERVER['SCRIPT_FILENAME'];
		$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
		$list = array(
			"$dir/templates/" . $this->templateName . "/$presenter/@$layout.latte",
			"$dir/templates/" . $this->templateName . "/$presenter.@$layout.latte",
		);
		do {
			$list[] = "$dir/templates/" . $this->templateName . "/@$layout.latte";
			$dir = dirname($dir);
		} while ($dir && ($name = substr($name, 0, strrpos($name, ':'))));
		return $list;
	}


	/**
	 * Formats view template file names.
	 * @return array
	 */
	public function formatTemplateFiles()
	{
		$this->setTemplateName();
		$name = $this->getName();
		$presenter = substr($name, strrpos(':' . $name, ':'));
		//$dir = dirname($this->getReflection()->getFileName());
		$dir =$_SERVER['SCRIPT_FILENAME'];
		$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
		return array(
			"$dir/templates/" . $this->templateName . "/$presenter/$this->view.latte",
			"$dir/templates/" . $this->templateName . "/$presenter.$this->view.latte",
		);
	}

	private function setTemplateName(){

		// using the templateName from neon config name
		//$templateName = $this->context->getParameters()['templateName'];

		// getting the template name from the default database in table templates
		$context = $this->context->getService('database.default.context');
		$selection = $context->table('templates')
		->where('default = 1');
		// TODO check if query has a result
		// TODO check if the directory exsits
		$this->templateName = $selection[0]->name;

	}
}

I overwrite the two functions formatTemplateFiles() and formatLayoutTemplateFiles() and added a private function setTemplateName() which is called at the beginning of the two overwritten functions.

The function setTemplateName() is getting the default database connection from the context, makes a query on the table “templates” and overwrites the private property $templateName.

The two functions formatTemplateFiles() and formatLayoutTemplateFiles() are now looking for a directory under the base www directory where the index.php is located ($_SERVER[‘SCRIPT_FILENAME’]) for a directory /templates/templateName.

I know, that there are some things to improve but it works.

Last edited by tomcat4x (2015-07-02 14:36)

alnux
Member | 106
+
0
-

hi there, on this proyect the system select template automatically by country (github file)