Jump to content
Wanze

Module: Smarty for the TemplateEngineFactory

Recommended Posts

TemplateEngineSmarty

This module adds Smarty as engine to the TemplateEngineFactory.

Screenshot of the available configuration options:

post-582-0-80828400-1404421508_thumb.png

Project on GitHub: https://github.com/wanze/TemplateEngineSmarty

Project in modules directory: http://modules.processwire.com/modules/template-engine-smarty/

Only Smarty related things should be discussed in this thread.

For common problems/features/questions about the Factory, use the TemplateEngineFactory thread.

Edit: I added a section to the readme on GitHub how I think Smarty works best in combination with the TemplateEngineFactory and ProcessWire. I used this technique in a recent project and I'm very happy with it.

  • Like 4

Share this post


Link to post
Share on other sites

Hi wanze,

I am trying to use the MenuBuilder module for the past one week along with smarty template..

The below is used in header.tpl (smarty template)

<nav class="menu">

    {$menu = $modules->get("MarkupMenuBuilder")}
    {$menu->render('testmenu')}

</nav>

I am able to generate the menu but without being able provide css class for the submenus,

however as soon i add $options as mentioned in MenuBuilder docs like below

<nav class="menu">
         {$options = array(
         'wrapper_list_type' => 'ul',
         'list_type' => 'li',
         'menu_css_class' => 'type-1',
         'submenu_css_class' => 'dropmenu',
         'current_class' => 'active',
         'default_title' => 0,
         'include_children' => 4,
         'm_max_level' => 1,
         'current_class_level' => 1,
                )}

         {$menu = $modules->get("MarkupMenuBuilder")}
        {$menu->render('testmenu', $options)}
</nav>

i get this error

Notice: Trying to get property of non-object in D:\xampp\htdocs\pw\site\modules\TemplateEngineSmarty\TemplateEngineSmarty.module on line 145

Fatal error: Exception: Syntax error in template "D:\xampp\htdocs\pw\site\templates\views\partials\header.tpl" on line 16 "'wrapper_list_type' => 'ul'," - Unexpected " => ", expected one of: "","" , ")" (in D:\xampp\htdocs\pw\site\modules\TemplateEngineSmarty\TemplateEngineSmarty.module line 109) #0 D:\xampp\htdocs\pw\site\modules\TemplateEngineFactory\TemplateEngineFactory.module(100): TemplateEngineSmarty->render() #1 D:\xampp\htdocs\pw\wire\core\Wire.php(459): TemplateEngineFactory->hookRender(Object(HookEvent)) #2 D:\xampp\htdocs\pw\wire\core\Wire.php(333): Wire->runHooks('render', Array) #3 D:\xampp\htdocs\pw\wire\modules\Process\ProcessPageView.module(187): Wire->__call('render', Array) #4 D:\xampp\htdocs\pw\wire\modules\Process\ProcessPageView.module(187): Page->render() #5 [internal function]: ProcessPageView->___execute(true) #6 D:\xampp\htdocs\pw\wire\core\Wire.php(398): call_user_func_array(Array, Array) #7 D:\xampp\htdocs\pw\wire\core\Wire.php(333): Wire->runHooks('execute', Array) #8 D:\xampp\htdocs\pw\ind in D:\xampp\htdocs\pw\index.php on line 248
Error: Exception: Syntax error in template "D:\xampp\htdocs\pw\site\templates\views\partials\header.tpl" on line 16 "'wrapper_list_type' => 'ul'," - Unexpected " => ", expected one of: "","" , ")" (in D:\xampp\htdocs\pw\site\modules\TemplateEngineSmarty\TemplateEngineSmarty.module line 109)

#0 D:\xampp\htdocs\pw\site\modules\TemplateEngineFactory\TemplateEngineFactory.module(100): TemplateEngineSmarty->render()
#1 D:\xampp\htdocs\pw\wire\core\Wire.php(459): TemplateEngineFactory->hookRender(Object(HookEvent))
#2 D:\xampp\htdocs\pw\wire\core\Wire.php(333): Wire->runHooks('render', Array)
#3 D:\xampp\htdocs\pw\wire\modules\Process\ProcessPageView.module(187): Wire->__call('render', Array)
#4 D:\xampp\htdocs\pw\wire\modules\Process\ProcessPageView.module(187): Page->render()
#5 [internal function]: ProcessPageView->___execute(true)
#6 D:\xampp\htdocs\pw\wire\core\Wire.php(398): call_user_func_array(Array, Array)
#7 D:\xampp\htdocs\pw\wire\core\Wire.php(333): Wire->runHooks('execute', Array)
#8 D:\xampp\htdocs\pw\ind

This error message was shown because site is in debug mode ($config->debug = true; in /site/config.php). Error has been logged.

I am not sure what i am doing wrong here  ?????

The errors seems to be about how i am calling the options using the smarty tags 

Share this post


Link to post
Share on other sites

Hi cenatur78,

Try to do the logic of building your menu in the "controller", not in the smarty template. Then you can pass the markup to the smarty template, e.g. $view->menu = $menu->render('testmenu');

Cheers

Share this post


Link to post
Share on other sites

I think there's a limitation in this module:

Let's imagine this scenario I have a page with fetches images and i need to do a thumbnail resize for each image however I do not know how to register a function with this module as in Smarty i can handle that but i don't have access to the current Smarty context, so right now I'm stuck, if there's a way i can my defined PHP functions in the smarty template, i'd appreciate it.

Thanks

UPDATE: It seems this works but we might need to expose some features to allow registering of functions, mind if i fork and send a PR ? 

class HomeController{

public function getTitle($page){
return $page->title;
}
}
$home =new HomeController();

$view->set("title",$title);
$view->set("content",$page->body);
$view->set("images",$page->sliderImages);
$view->set("controller",$home);

Share this post


Link to post
Share on other sites

Thanks i think this is the best module ever, I can't build any Processwire anymore unless i use this Smarty module, always had issues with traditional Processwire templating but this is dynamic, thanks for the response my eyes didn't catch that method.

  • Like 1

Share this post


Link to post
Share on other sites

Haha, same feelings here with another template engine. Once you get the hang of it there's no way back :)

  • Like 1

Share this post


Link to post
Share on other sites

I used the Smarty template module to build a site the flexibility and block system reminds of MasterPages in ASP.NET  

Share this post


Link to post
Share on other sites

any way to use Ajax in this smarty ? like stopping when it's ajax and just returning a JSON 

Share this post


Link to post
Share on other sites

Hey, is there any way to support template inheritance programmatically? Right now, we are limited to calling

{extends file='html.tpl'}

in the first line of every template we want to inherit from html.tpl.

However, this can also be done inside the smarty fetch call, see: http://www.smarty.net/docs/en/resources.extends.tpl

Thanks for the awesome module though :) 

Share this post


Link to post
Share on other sites

@Wanze, I ping you, as the above question seems to be for you. ;)

Share this post


Link to post
Share on other sites

@urz0r

Not at the moment, how would you use this function with the module, can you make an example?
As for now, you can hook after TemplateEngineSmarty::initSmarty() and customize the Smarty object, but I guess that is not useful in this situation.

Cheers

Share this post


Link to post
Share on other sites

Hi I noticed during development of Modules, if i attempt to use Process::setViewFile it calls the current TemplateFactoryEngine which in my case is the Smarty Engine and attempts to look for a view in the site, I want to create modules with external templates rather than having templates in the code. I want to ask if there's a current way to stop this action. I will research and find out but i won't mind pointers thanks. 

Share this post


Link to post
Share on other sites

I'm sorry for the empty post above; somehow I'm not able to write in the editor when editing ??? And submitting the post was by mistake :lol:

I don't understand your question, could you post some example code and explain in more detail what's the problem? The module should only attempt to render your page with smarty, if there exists a "myTemplate.tpl" smarty template to the corresponding "myTemplate.php" ProcessWire template.

Share this post


Link to post
Share on other sites
11 hours ago, Wanze said:

I'm sorry I will delete the post above, somehow I'm not able to write when editing and the posting was by mistake ???

I don't understand your question, could you post some example code and explain in more detail what's the problem? The module should only attempt to render your page with smarty, if there exists a "myTemplate.tpl" smarty template to the corresponding "myTemplate.php" ProcessWire template.

This is so awkward, The issue was sorted out but i actually forgot to reply to this forum as i didn't get any notification. thanks 

  • Like 1

Share this post


Link to post
Share on other sites

Hello, in the settings page I'd like to know what "Global Template File - Filename of a template file that is used as main template behind the API variable" is for. I tried to make up a layout.tpl and use the file name as field setting, but looks like {block} contents are not passed to the file.

Thanks!

Share this post


Link to post
Share on other sites
6 hours ago, Manaus said:

Hello, in the settings page I'd like to know what "Global Template File - Filename of a template file that is used as main template behind the API variable" is for. I tried to make up a layout.tpl and use the file name as field setting, but looks like {block} contents are not passed to the file.

Thanks!

This means that you will have the same smarty template behind the $view variable. Normally, each controller (ProcessWire template) has its own template, e.g.

home.php --> home.tpl
basic-page.php --> basic-page.tpl

However, with smarty you do not need this feature. You can make use of template inheritance, so create a global "layout.tpl" and define your blocks. Then, in your child templates you can overwrite these blocks.

Hope it helps! Cheers

Share this post


Link to post
Share on other sites

Thanks Wanze, still I noticed that if I use the Global File instead of the {extends} instruction, no block is being passed. Anyone tested this happening?...

Share this post


Link to post
Share on other sites

@Manaus Can you provide some example code? Unfortunately I do not understand what you mean by "no block is being passed". Is it possible you are mixing up features of the template engine with the module? In smarty, you use the {block} tags to define sections that are either inherited or overwritten in a child template. To overwrite a block, you must use {extends} in your child template. If you provide some code I'm sure I can help you :)

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

@wanze Global file should make {extends} useless if I understood right. That's because in child templates {extends} works also if no Global File is specified in the admin pane. I hope I clarified the issue...

Share this post


Link to post
Share on other sites

 

20 hours ago, Manaus said:

@wanze Global file should make {extends} useless if I understood right. That's because in child templates {extends} works also if no Global File is specified in the admin pane. I hope I clarified the issue...

No :) The global template file has nothing to do with smarty, it is a feature provided by the module that is available for all connected template engines. The global template file only means that you always get the same (smarty|twig|...)-template behind the $view API variable. As already said, you probably don't want to use a global template file with smarty, because you can use smartys template inheritance which is much more powerful. For me, this is the main reason to use a template engine.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks Wanze, sorry I'm a bit dumb, what do you mean by "behind the $view API variable"?

Share this post


Link to post
Share on other sites
On 14.8.2017 at 5:50 PM, Manaus said:

Thanks Wanze, sorry I'm a bit dumb, what do you mean by "behind the $view API variable"?

I meant the active smarty template when you pass data in your controllers, e.g. $view->set('title', $page->title);

The convention of the module is that the smarty template has the same name as the controller, e.g. home.tpl for your home.php file.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...