Announcement

Collapse
No announcement yet.

How to affect variables in template hooks?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to affect variables in template hooks?

    Greetings,

    How to change the value of a variable in a template hook that affects the variable in the parent that calls the hook?

    Setup is...

    In Template:
    conversation_comment_item

    Contains this:
    {vb:template conversation_footer, conversation={vb:raw conversation}, controls={vb:raw footerControls}}

    In Template:
    conversation_footer

    Contains this:
    {vb:hook 'conversation_footerright'}

    So template conversation_comment_item calls template conversation_footer and passes footerControls as controls.

    conversation_footer contains the hook: conversation_footerright

    I added a template hook:
    Hook Location: conversation_footerright
    Template Name: hook_change_vote_ctrl
    Hook Arguments: controls=controls

    Created template: hook_change_vote_ctrl

    Which contains only this...
    {vb:set controls.showVoteCtrl, 1}

    All this works as expected, except it has no effect on the "controls" variable in the parent template.

    So the hook system does not allow changes to variables just using templates and hooks?

    Or did I missing some step?

    --

    https://www.vbulletin.com/docs/html/...emplate_syntax
    {vb:debugvardump}

  • #2
    You can pass variables to your custom template using the Hook Arguments field in the Template Hook Manager.

    Use this field to enter any arguments to be passed from the parent template to the called template.
    Enter each variable assignment on a new line as "varname=var-to-pass". For example

    userinfo=userinfo
    userid=userinfo.userid

    Note that only variables can be passed, use the dot syntax to pass array elements.
    Note, the dot syntax would be array.key not $array['key'];

    However, templates don't send values between each other. Think of each template as a PHP function or method. Its variables are Local to it. You should set the variable in the template it is needed in.
    Translations provided by Google.

    Wayne Luke
    The Rabid Badger - a vBulletin Cloud demonstration site.
    vBulletin 5 API

    Comment


    • #3
      Thanks for the info.

      But as I mention in the post:

      Hook Arguments: controls=controls

      That works as expected. In the template, I can view and change values. All good.

      But, any changes to the values are lost in the calling template.

      Start in template: conversation_footer
      Hook in that template: conversation_footerright

      Template hook:
      Hook Location: conversation_footerright
      Template Name: hook_change_vote_ctrl
      Hook Arguments: controls=controls

      In template hook_change_vote_ctrl, hange controls value: {vb:set controls.showVoteCtrl, 1}

      But... back in conversation_footer, the change is lost.

      I used {vb:debugvardump} in both templates to verify that the change has not effect.

      Comment


      • #4
        You could not set variables in hook templates and give those back to the parent. It is one way from parent to hooked child.
        This could only been done by api. But only if templates not save as files.

        Comment


        • #5
          Originally posted by raywjohnson View Post
          That works as expected. In the template, I can view and change values. All good.
          The child template, the one called in the hook, only modifies its local version of the variable. It does not modify the version passed to it. This is by design and actually how most programming languages work.

          However, templates don't send values between each other. Think of each template as a PHP function or method. Its variables are Local to it. You should set the variable in the template it is needed in.
          When you add controls=controls to the template hook call, the PHP API in vBulletin makes the value available as a local template variable through a registration process.

          You should set your value in the conversation_footer template.
          Translations provided by Google.

          Wayne Luke
          The Rabid Badger - a vBulletin Cloud demonstration site.
          vBulletin 5 API

          Comment


          • #6
            Thanks everyone for the information and insight. I will use the API to solve this.

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...
            X