TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Yet another mustache implementation
  • All,

    I've uploaded a class "Generator" to github which merges templates with a data dictionary.

    The syntax is an extended mustache syntax which I've been using for several months now and seems very solid.

    The class itself is (almost) standalone (just uses Kohana::find_file(...)), and quite concise (around 500 lines), stable for months now. I use it for a relatively complex application of my emerging CMS at publiccommons.ca.

    The main benefit I find of this approach (templates), is that the clean separation between template and dictionary (view and model), promotes clean controller sequences.

    One tip: for rendering options I find it helpful to create global state and permission variables (like "is_admin_mode" and "can_edit"), which can then be used in assertions to control the layout rendered for the particular user.

    Markup Notes:

    • * = global = reference to dictionary root associative array
    assertions (act like IF):   
        {{?*...}} ... {{/?*...}} global var assertion
        {{?...}} ... {{/?...}} var assertion
        {{#?*...}} ... {{/#?*...}} global section assertion
        {{#?...}} ... {{/#?...}} section assertion
    namespaces (nested sections):
        {{#*...}} ... {{/#*...}} global section
        {{#...}} ... {{/#...}} section
    inversions (act like IF NOT):
        {{^*...}} ... {{/^*...}} global inverted section or variable
        {{^...}} ... {{/^...}} inverted section or variable
    specialized:
        {{>...}} partial template
        {{!...}} comment
    variables:
        {{&*...}} unescaped global var
        {{*...}} global var
        {{&...}} unescaped var
        {{...}} variable
    

    dictionary:

    • root must be associative array
    • nested values can be
      • basic datatypes, or
      • simple objects (interpreted as nested namespaces, converted to associative arrays)
      • list arrays, which are iterated with parallel section template fragment

    Usage:

          $contentview = Generator::view(array($contenttemplatefile),$contentdictionary);
          $pageview = Generator::view(array($pagetemplatefile),$pagedictionary);
          $pageview->content = $contentview;
          $this->response->body($pageview);
    

    Template fragments and dictionary are processed and recursed in tandem.

    • Henrik
  • Here's some markup usage:

    Hello {{firstname}}{{^is_marketing_letter}},{{/^is_marketing_letter}} {{?is_marketing_letter}}{{lastname}},{{/?is_marketing_letter}}
    
    {{#?recordset}}
       <p>Here is a list of your recent purchases:</p>
    
    {{#recordset}}
    {{>purchase_record_template}}
    {{/#recordset}}
    
       <p>Total Purchases: {{totalpurchases}}</p>
    
    {{/#?recordset}}
    
       <p>We have some exciting news ...</p>
    
  • I've renamed the repo "Template" (and can't edit the previous post).

    https://github.com/HenrikBechmann/Template (and I intend to rename the class to "Template"...)

  • Cool. Thanks.

  • OK, I've renamed Generator to Template, so the code example now reads:

      $contentview = Template::view(array($contenttemplatefile),$contentdictionary);
      $pageview = Template::view(array($pagetemplatefile),$pagedictionary);
      $pageview->content = $contentview;
      $this->response->body($pageview);
    
    • Henrik

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion