Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

How to extend Context in custom helper? #148

Closed

Description

@jcubic

I wan to create custom helper sql:

<ul>
{{#sql SELECT * FROM users}}
   <li>{{username}}<br/>
   <ul>
      {{#sql SELECT * from files where user.id = [id]}}
          <li>{{filename}}</li>
      {{/sql}}
    </ul></li>
{{/sql}}
</ul>

how can I use Context in custom helper to make it work? Also if I'm in helper and call $context->get('id'); will it return the id from sql higher in hierarchy? Do I need to modify Helpers.php to add this helper or is there a way to do it outside of the library in user code?

Activity

  1. bp-behrooz commented on Aug 14, 2016

    @bp-behrooz
    Contributor

    First of all, bundling the logic and markup is not a good idea. If that's what you want, embedding html and php in one file would be much better option. Specially because of its performance compared to what you get from this library.

    However, if you in really want to do this, have a look at ChildContext. Basically, you need to extend it and provide the data that you want to it. If a variable didn't exist in your own implementation, you fall back to parent context.

  2. bp-behrooz commented on Aug 14, 2016

    @bp-behrooz
    Contributor

    Also you might find #132 (comment) interesting. That's another way to approach this.

  3. jcubic commented on Aug 15, 2016

    @jcubic
    Author

    You already have logic in handlerbars like #each and #if, so I don't think there is the reason why not to add just one more logic helper.

  4. JustBlackBird commented on Aug 15, 2016

    @JustBlackBird
    Contributor

    #each and #if aren't the same as #sql. Also, running database queries from templates is a very-very bad practice in any template engine community. Even if you can do so you definitely should not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions