Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Can you use PHP in default.master.tpl?

24

Comments

  • edited March 2011
    I'm guessing Vanilla is doing some funky stuff with these includes. They work great if the file I am including is in the same folder as default.master.php (i.e. the views subfolder of my theme). It also works if the file is in another subfolder of the theme and I use ../subfolder/file.php. If I try to move the file to the theme root or any higher up the tree, it refuses to include it.

    I suppose I can get live with that, but it means I have to move my website wrappers into my Vanilla theme instead of where they belong ... in the website theme.

    Vanilla also strips out unclosed div tags from these includes, which is crummy. It's very common when writing wrappers to open a div in a header and then close it in a footer. Vanilla seems to be forcing me to move the wrapper portion into every file that uses the header and footer, which sort of defeats the purpose.
  • edited March 2011
    I must have had my folders crossed or perhaps it was my eyes. I thought the includes were relative to the views folders, but they are relative to the root folder of the forum, which means I CAN put my include files outside of the forum - great.

    Also since Vanilla strips the unclosed div tags, I can leave then in the header and footer where they are used throughout the rest of the site, and only need to add them the theme files, which is manageable.
  • If you still are interested in including PHP within your template you can use the {php} built-in function.
    smarty.net/docs/en/language.function.php.tpl

  • no with the default settings. not all smarty tags are enabled.

    Also if you are including php, you might as well copy default.master.php over and use that instead. Using php in a template language defeats the point of using a template language.

    grep is your friend.

  • chanhchanh OngETC.com - CMS Researcher ✭✭

    I can't seem to find this file in 2.1b2.

    /applications/dashboard/views/default.master.php

    Where is it?

    Thanks

  • LincLinc Detroit Admin

    2.1 removes default.master.php in favor of default.master.tpl. You can still use a default.master.php in your themes. It's not possible to leave it in place as an example because .php files take precedence over .tpl.

  • chanhchanh OngETC.com - CMS Researcher ✭✭

    How would I convert .tpl into .php so I can use it to make sure it use the same layout of the theme that I use?

    Thanks

  • R_JR_J Ex-Fanboy Munich Admin
    edited April 2014

    There are some discussions concerning smarty:
    http://vanillaforums.org/discussion/comment/202016#Comment_202016
    http://vanillaforums.org/discussion/comment/203097#Comment_203097
    Understanding how Smarty is working might help, also: http://vanillawiki.homebrewforums.net/index.php/Using_Smarty_with_Vanilla

    And using the code in that directory https://github.com/vanillaforums/Garden/tree/2.1/library/vendors/SmartyPlugins should be all you need to create a default.master.php (but you can use php in tpl, so it might not be necessary)

  • chanhchanh OngETC.com - CMS Researcher ✭✭

    oh, I see.
    Thanks for the links and I will go over them.

  • chanhchanh OngETC.com - CMS Researcher ✭✭

    I might miss something I don't see any reference on how to use php in tpl.
    Please point me to the right direction.
    Thanks

  • businessdadbusinessdad Stealth contributor MVP
    edited April 2014

    @chanh said:
    I might miss something I don't see any reference on how to use php in tpl.

    I may sound pedantic, but the purpose of Smarty is precisely to avoid using PHP in templates. If you absolutely need to use it, then you should switch to a .php template.

    @chanh said:
    How would I convert .tpl into .php so I can use it to make sure it use the same layout of the theme that I use?

    Unfortunately, there is no hard and fast way to "port" a .tpl to .php. The only guideline I usually follow is "see what the .tpl does and do the same in .php", which means analysing the template, finding the various calls to Smarty functions and replacing them with the equivalent PHP calls.

  • peregrineperegrine MVP
    edited April 2014

    @chanh
    sometimes I wonder if you read the responses. :) r_j provided good links above

    you need to would need to write custom smarty functions for best results.

    pretty much either learn to make a function or switch entirely to default.master.php

    here are some more.

    http://www.smarty.net/best_practices
    http://www.smarty.net/docs/en/plugins.tpl
    http://www.smarty.net/docs/en/plugins.functions.tpl

    http://vanillaforums.org/discussion/comment/167432/#Comment_167432
    http://vanillaforums.org/discussion/comment/162608/#Comment_162608
    http://vanillaforums.org/discussion/20533/solved-documentation-smarty-templates-default-master-tpl-usage-question

    since php tags are deprecated.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    You can use a php based theme even if the one in the default theme is tpl

    I have converted themes from tpl to php and back.

    The tpl uses php but through theme hooks

    To convert a tpl master into php just use a normal master that is in php and add the parts missing like the Row and the MeBox .

    Look at the theme DarkPink default.master.php that is for 2.1 and is a php based theme made from a tpl.

  • chanhchanh OngETC.com - CMS Researcher ✭✭

    I have a specific need to run some php code and since the template I use is in tpl that is why I want to know how to run PHP in tpl.

    You all are so helpful.

    Thanks I am appreciated.

  • chanhchanh OngETC.com - CMS Researcher ✭✭

    I put this code in the .tpl but it is not executing.

    {php}
    echo "hello";
    {/php}

    Do I have to enable something in Vanilla in order for it to execute php code?

    Thanks

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    No you can't do that , you might be able to use {literal} php here{/literal} tags to get it in there I know it works for script but not sure if it is the same for php.

    The best thing is to use theme hooks to add php to the master. You need a themehooks file in your theme.

    Look at the library/vendors/and find the smarty folders there you can see how it works.

    The php is inside a smarty function . It is compiled there so you can use {link} stuff like that instead of the whole php.

    Or convert your template to php.

    Just clone a theme php master and then add a couple things like Row . Then save it as a master php.

  • chanhchanh OngETC.com - CMS Researcher ✭✭

    I was hoping for a shortcut to implement my php code but look like I have to do it without the shortcut.

    Now I have some idea where to look.

    Thanks for all the help.

  • peregrineperegrine MVP
    edited April 2014

    php tags is not recommended and deprecated and not best practices and yet you defy the recommendations.

    I have to laugh.

    you still don't read links posted. :):):)

    http://www.smarty.net/best_practices

    and I quote from the people who made the software

    Now lets get to the first problem: embedding PHP in templates.
    1. Do not embed PHP!

    This is by far the biggest mistake with Smarty templates. When Smarty was first developed, the {php} tag was provided as a sort of "last resort" way of solving a problem. Experience has revealed that directly embedding PHP is not only unnecessary, it creates more problems than it solves. The {php} tag is strongly discouraged, and is now deprecated in Smarty 3.

    Despite the discouragement, lazy developers continue to fall on the {php} crutch, as this may seem (at first glance) to be the quickest path to implementation. However, this approach just leads to more problems. The correct approach is to implement a plugin and do the PHP logic outside of the template.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    The correct approach is to implement a plugin and do the PHP logic outside of the template.

    Which is what the class.themehoooks.php file is, a plugin for the theme template :)

Sign In or Register to comment.