HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

What's the easiest method to assign value from PHP to smarty?

Hey,

as in title, i am looking for the easiest way to assign the value from php to .tpl file in smarty. Already read several topics i found on this forum but none of them works.

And yes, I have created "class.mythemethemehooks.php" file in mian custom theme folder but seems like whatever i do in this file, does not affect default.master.tpl template.

Would be great if anyone can help.

Thanks in advance!

«1

Comments

  • Something like

    public function base_render_before($sender) {
        $sender->setData('someName', $value);
    }
    

    should be the solution

  • Thanks @R_J . Should I locate functions for smarty in a specific file? If I will create the function as above, in .tpl should i use it like {someName} and that's it?

  • There is no single best solution. In the end it depends what you want to achieve. If we are talking about smarty functions, than please search the forum. I think I have started a discussion in the tutorial section about creating custom Smarty functions. But I would only do that a) if this is a complex function and b) it is used in multiple places on your page with different input. Otherwise I would suggest using the themehooks file.

    But using base_render_before is not the best solution. Most of the time there are better events that you can hook into. If you give more details, I'll might be able to give better advice.


    I have never done that much with Smarty, but this is simple enough to simply try it out ;-)


    By the way: if you create a theme and afterwards add a themehooks file, you need to clear /cache/themes and if you change the default.master.tpl, it doesn't hurt to clear the /cache/smarty folder, too.

  • @R_J What I would like to do is basically pass some variables or function results to smarty so i can use it in .tpl files. That's all i would like to use. I did created theme hooks file according to your suggestion in other topic and the file is located in main folder of theme and its name is: class.MyThemeNamethemehooks.php

    Inside this file, i have the following code:

    <?php defined('APPLICATION') or die();
    
    /**
     * Sample implementation of a theme hooks class to show
     * the use of custom Smarty plugins.
     */
    class MyThemeNameThemeHooks implements Gdn_Plugin {
      /**
       * Setup function is needed for this class, so don't delete it!
       *
       * @return bool Dummy return value.
       */
      public function setup() {
        return true;
      }
    
      /**
       * This function hooks the Smarty init to add our directory
       * containing our custom Smarty functions
       *
       * @param object $sender Smarty object.
       * @return void
       */
      public function gdn_smarty_init_handler($sender) {
        // add directory "/themes/MyTheme/SmartyPlugins/"
        $sender->plugins_dir[] = dirname(__FILE__).DS.'SmartyPlugins';
      }
    
      public function base_render_before($sender) {
        $sender->setData('someName', 'custom value');
      }
    
      // whatever...
    }
    

    but unfortunately, it does not work. When I try to use {someName} in default.master.tpl it simply does nothing. Thus, I wonder what i do wrong exactly.

  • Please search some more discussions on this. There has been a change how plugin folders must be added. I'm on mobile so I leave the searching part to you, sorry

  • warzydwarzyd New
    edited November 2020

    I know that topic, this is where from i get above content ;) You refer to https://open.vanillaforums.com/discussion/27767/how-to-create-custom-smarty-functions

    But my question is, if it is possible to pass just simple variables from functions located in class.MyThemeNamethemehooks.php file without moving each function to separate file in /SmartyPlugins/ folder ;)

  • This is the comment that you should look at:


    Which page do you want to change or even better: which part of the interface do you want to inject values? As I said before: with base_render_before and setData you should be able to inject values to the default.master.tpl

    But a) using base_render_before for all purposes is waste of resources and b) changing default.master.tpl is rarely needed

    Normally you just intercept the view file and inject code via the hooks

  • warzydwarzyd New
    edited November 2020

    Normally you just intercept the view file and inject code via the hooks

    Do you know any tutorial where i can find necessary details about that?

  • Well, documentation sucks. It became even worse since Vanilla uses its own Knowledge Base: the current developers documentation states it is incompletes and references to an old version but that is just a link to that same site:

    The closest thing is here:

    Taking a look at the existing plugins can also be very helpful:


    But you can always ask questions here. Start by giving one detailed example of what you want to achieve (like e.g. adding post count below user name in discussions and comments), maybe with a screenshot and we will try to give you the code (that would need to be injected in the themehooks file

  • warzydwarzyd New
    edited November 2020

    Thanks for the links. Been trying to work on it but it does not assign variables from MyThemeThemeHooks class/file to smart. Later read the following https://success.vanillaforums.com/kb/articles/233-smarty-overview#accessing-controller-data-with-smarty and as it explained here, I should be able to display available smarty vars by simply pasting {debug_vars} into default.master.tpl file. Even this, does not work. All I get, you can see on the screenshot below.

    The file class.MySiteThemeHooks.php looks as below:

    But in default.master.tpl i just tried to call {debug_vars}:


  • It's only {debug}

  • You're right, it does work now ;)

    But why my custom value does not work? I mean {someName} in above example. It does display same error as above when used {debug_vars}

  • I was wrong with that base_render_before thing. Try this:


       public function base_beforeFetchMaster_handler($sender, $args) {
           $sender->setData('Hello', 'World');
       }
    


  • You would print it out with {$Hello}

  • Did tried above function instead and still does not work but seems to be closer to the final result. However, this time got different notice Undefined index: Hello and Trying to get property 'value' of non-object


  • Have you also seen my second comment? This is what I get:

    From this:


  • Yes, did exactly as you described.

    After further investigation, I noticed that the problem is with theme hooks file itself. I did everything according to https://success.vanillaforums.com/kb/articles/246-theme-hooks but seems like this file is not being taken into account.

    1. I created Open-Vf-ComThemeHooks.php file in main, custom theme folder which is "open-vf-com". On the following screenshot you can see how does it look like https://prnt.sc/vh0cok
    2. No matter what I am trying to do in above file, it simply does not have any effect on site. Seems like forum does not see this file. Not sure if I should maybe define this theme hook file in configuration or do something else?
  • First of all: if you create a theme which has no themehooks file and add that file later, you need to delete the content of /cache/themes.

    Try to omit the dashes in the file and the class name: "OpenVfComThemeHooks" (delete the cache/themes folder after renaming class or file name).

    If that doesn't work add the following line to your addon.json: "className": "OpenVfComThemeHooks",and don't forget to delete the /cache/themes folder afterwards ;-)

  • Thanks @R_J, you are the best ;) Removing the dashes in the file and class name, and later clearing cache/themes/ folder did the trick. Seems like cache was the main reason. Had no idea about that 🙈

  • eastereggeasteregg New
    edited June 2021

    Sorry in advance for "hijacking" this thread - but I have the same question and following your steps in Version 2021.009 didn't work out for me.

    So here is where I am at:

    • Lets call the theme "MyTheme"
    • It has its folder under themes/mytheme/, including the files addon.json and a MyThemeThemeHooks.php
    • Also in this folder: A subfolder /views/ with a default.master.tpl - and since I've worked with Smarty before, I've build my forums homepage with the Smarty vars available (try&error with the help of {debug_vars}) [after reading into my issue, I am now aware this is not the "recommended way of doing it", and I should rather use CSS 😅]

    So what do I want to achieve: Simply having the {$Hello} variable available in Smarty, ideally with my test value "World". Every time I've emptied the /cache/theme/ folder.

    Here my unsuccessful attempts at figuring it out via the MyThemeThemeHooks.php:

    #1

    <?php defined('APPLICATION') or die();
    
    class MyThemeThemeHooks extends Gdn_Plugin {
    
      public function setup() {
        return true;
      }
    
      public function base_render_before($sender) {
        $sender->setData('Hello', 'World');
      }
    
    }
    

    #2

    <?php
    
    class MyThemeThemeHooks extends Gdn_Plugin {
    
      public function setup() {
        return true;
      }
    
      public function base_beforeFetchMaster_handler($sender, $args) {
        $sender->setData('Hello', 'World');
      }
    
    }
    

    #3 (here you can see I became creative)

    <?php
    
    class MyThemeThemeHooks extends \Gdn_Plugin {
    
      public function base_render_before($sender) {
        $sender->setData("Hello", "World")
      }
    
    }
    

    I've also checked the mytheme.php, which gets created in the cache folder - and it references the MyThemeThemeHooks...

    Basically I had reached the point, where I had disbelief in my default.master.tpl - maybe I was missing a hook, or such. So I wanted to check another theme, Keystone: Since there also setData is used, I simply added a {debug_vars} to the template - and to my surprise, also there the custom setData didn't end up in Smarty.


    Long story short: Any ideas what I did wrong?

    The last 3 days I did stare at my code, but I am out of ideas. Maybe simply the sender-function is deprecated now?

    Thanks in advance for any ideas

Sign In or Register to comment.