Jump to content

A way to suppress loading of templatefile?


NorbertH
 Share

Recommended Posts

Is there a way of suppressing the automatic  loading of the Templatefile?

I was experimenting with Somas template approach

https://processwire.com/talk/topic/740-a-different-way-of-using-templates-delegate-approach/

The problem is that you have to bend all Templates to a single file.

I thought of putting  the script into the _init.php($config->prependTemplateFile) That way you can use the default template structure. But if i do this, all templates get included again after _init.php is loaded. So now i am thinking about if there is a way to suppress that additional loading.

Link to comment
Share on other sites

First, i always  tend to experiment a lot :biggrin:, helps me understand how things might work.

Second i want to use one file, namely the _init.php but i don't want to break the default template handling mechanism inside the backend.

Third, i like the idea of having one central file to allow for template wide functions , for example a loginscript that allows me to have a loginbox on every page and don't need any redirects as  i simply can post the form to the current page itself.

Fourth, maybe its possible via hooks , so again its about learning how things work ;-)

Link to comment
Share on other sites

The default siteprofile of version 2.5 does exactly what you want. Each template calls it's own template.php file. With prependTemplateFile the _init.php is called before the template.php to have global variables, sidebars and stuff, from there you can include a _func.php with global functions, too. After that the template.php fills the template specific variables and they are all echo'd in the _main.php, which is either included at the end of the template.php or with appendTemplateFile. 

  • Like 6
Link to comment
Share on other sites

@mr-fan and LostKobrakai

I already know that approach but it need to wirte the content into variables either by using outputbuffer or by suing something like
 

$sidebar .= "<ul class='nav'>";
  foreach($page->children as $child) {
    $sidebar .= "<li><a href='$child->url'>$child->title</a></li>";
  }
  $sidebar .= "</ul>";

Which i don't like very much in templating as it just not fells like a template that way.(at least not to me !)
Feels like mixing code and HTML instead of templating.

Another option is that you use each templatefile as a dispatcher in its onw that has an additional file(same name) inside the markup folder very much like in the blog profile.

Just having empty templatefiles in the templatefolder  and then following Somas approach whith a view folder would solve the problen but still i then have redundant files lying around on the server .

Link to comment
Share on other sites

ok i think for templates in semantic way you've two options....

use PHP + HTML while PHP + HTML = dynamic content.....while the PW API give the power to minimize footprint and make it readable.

or use a template engine with controllers like:

https://processwire.com/talk/topic/6835-module-twig-for-the-templateenginefactory/

or

https://processwire.com/talk/topic/4604-template-data-providers/

that would be the option to get rid of php tags in HTML....

oh third way i found on many examples (like in joss's newstutorial)

Third, i like the idea of having one central file to allow for template wide functions , for example a loginscript that allows me to have a loginbox on every page and don't need any redirects as i simply can post the form to the current page itself.

build up a _func.php for some basic functions to generate your output for navigation, login or something else and reuse it in any templates.

example from my first project:

1. this in my _init.php

// Include shared functions
include_once("tpl/_func.php");

2. and this is a part of the _func.php

<?php

/**
 * /site/templates/_func.php
 *
 * Example of shared functions used by template files
 *
 * This file is currently included by _init.php
 *
 */

//default vars
$homepage = $pages->get("/");

//siteMapNav
$siteMap = $modules->get("MarkupSimpleNavigation"); // load the module
$siteMapNav = $siteMap->render(); // render default menu

//mainMenuNav
$mainMenu = $modules->get("MarkupSimpleNavigation"); // load the module
	$options = array(
		'parent_class' => 'parent',		// overwrite class name for current parent levels
		'current_class' => 'current',	// overwrite current class
		'has_children_class' => 'has_children',    // overwrite class name for entries with children
		'levels' => true,    // wether to output "level-1, level-2, ..." as css class in links
		'levels_prefix' => 'level-',    // prefix string that will be used for level class
		'max_levels' => 1,    // set the max level rendered
		'firstlast' => false,    // puts last,first class to link items
		'collapsed' => false,    // if you want to auto-collapse the tree you set this to true
		'show_root' => true,    // set this to true if you want to rootPage to get prepended to the menu
		'selector' => '',    // define custom PW selector, you may sanitize values from user input
		'selector_field' => 'nav_selector',    // string (default 'nav_selector') define custom PW selector by using a property or field on a page. Use this setting if you want to overwrite the default nav_selector
		'outer_tpl' => '<nav><ul class="nav-list">||</ul></nav>',    // template string for the outer most wrapper. || will contain entries
		'inner_tpl' => '<ul>||</ul>',    // template string for inner wrappers. || will contain entries
		'list_tpl' => '',    // template string for the items. || will contain entries, %s will replaced with class="..." string
		'list_field_class' => '',  // string (default '') add custom classes to each list_tpl using tags like {field} i.e. {template} p_{id}
		'item_tpl' => '<li><a class="pure-button"  href="{url}">{title}</a></li>',    // template string for the inner items. Use {anyfield} and {url}, i.e. {headline|title}, if field is of type image it will return url to image (first image if multiple)
		'item_current_tpl' => '<li class="selected"><a class="pure-button" href="{url}">{title}</a></li>',    // template string for the active inner items.
		'xtemplates' => '',    // specify one or more templates separated with a pipe | to use the xitem_tpl and xitem_current_tpl markup
		'xitem_tpl' => '<a href="{url}">{title}</a>',    // same as 'item_tpl' but for xtemplates pages, can be used to define placholders
		'xitem_current_tpl' => '<span>{title}</span>',    // same as 'item_current_tpl' but for xtemplates pages
		'date_format' => 'Y/m/d',    // default date formatting for Datetime fields and native created/modified
		'code_formatting' => false,    // enable or disable code indentations and newslines in markup output
		'debug' => false,    // show some inline information about rendertime and selectors used as html comments
	);
$mainMenuNav = $mainMenu->render($options);

should be no problem to set such functions as controller for a templatefile like soma to get the controller in /views/ to setup needed functions....so you've got one main.php template with the main HTML structure....templatefiles for your different output (HTML of the content) and some reusable included functions to clean up the templatefiles...

But you've got one file more anyway.

I've choose to have on _func.php that is included and give me some helpers for all templatefiles it's all there a Processwire foreach isn't that hard abstract for normal templates and for more build a function.

http://processwire.com/api/why-php-syntax/

i like this one ;)

  • Like 1
Link to comment
Share on other sites

At least for this simple question you gone far to far, maybe you misunderstand me.

I just prefer the alternative php syntax as it feels far more like templating.

(plus it looks a lot like other templating language)

<? foreach ($file as $f) : ?>

... some stuff ...

<? endforeach; ?>

but to put this into a variable you need to use output buffer.

Using the conventional syntax in templates feels entirely wrong, at least for me.

I am no template engine freak that likes to remove php from templates just to add php tags like smarty does.

Its really nice of you trying to explain the concepts of PW templating but i am pretty sure i already know whats going on in PW templates.

The only thing i really want to know if it is possible to suppress the default call for the template file but keep the _init.php and _done.php call.

It may be that it allows a way to an interesting templating alternative or maybe at least one that fits my personal needs best.

Link to comment
Share on other sites

Hhm, I cannot really follow, and sure I have misunderstood you. But when I read "to put this into a variable you need to use output buffer", my brain rings alert, and if I want or not: I cannot follow further.

Here in the forums in the last past years were so many different aproaches presented and discussed, mostly from people who know PW really good because they have used it for many Projects. I'm not aware of any that needs output buffering. (sounds ugly to me)

It seems we think diametrically opposite. I'm afraid I can be of any help to you.

Edited by horst
make my conclusion more standing out
  • Like 2
Link to comment
Share on other sites

When it comes to loading templatefiles the core first loads the _init.php then the templatefile and finally the _done.php.

at least if

$config->prependTemplateFile = '_init.php';

$config->appendTemplateFile = '_done.php';

are set

I want it to not load the templatefile!

Link to comment
Share on other sites

Then again i break the backend functionality as i do whith Somas Template approach ;-)

btw. really lots of germans around here!

You are requesting not to load any template file, thats breaking the backend functionality. So why bother breaking any others?

Btw. which backend functionality was broken by doing that?

Yes, thats true! Many germans here!

Link to comment
Share on other sites

There must be a starting point for your output and every visible page needs a canvas to paint on. You could however assign a different different file (Very old Soma's approach). How you structure your template and which files you include it is up to you. You could use the processwire's _init.php and _out.php approach, but I like to include my self. It's all freedom with the exception that your the data should land somewhere for visible output. 

For a big site (130+ templates) I've used a module Select File Inputfield to render parts of the selected file. The reason is to reduce the amount of templates.

Link to comment
Share on other sites

@NorbertH

If I understand you right, you don't like all those markup-string concatination, which gets stored in variables, like this:

foreach(){
  $out .= "markup";
}

I don't like it as well. At least for more advanced snippets of more than 2 or 3 lines. Your prefered way of php templating does output text directly which isn't great either for a deligated approach. That's why I'm using the TemplateFile class of ProcessWire. It let's you define a html-template in your prefered way, while most of the logic can stay in the processwire-page-template files. 

// tpl/tmp_article.php
<div class="article">
  <h2><?php echo $title; ?></h2>
  <?php echo $text; ?>
</div>
// articles.php
<?
$content = "";
foreach($pages->children as $article){
  $t = new TemplateFile($config->paths->templates . "tmp/tmp_{$page->template}.php");
  $t->set( "title", $article->title );
  $t->set( "text", $article->body );
  $content .= $t->render();
} 

As the template files are normal php files as well you can still do what you want to. The $t->render() method just returns everything that gets outputted by the template file.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...