DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
    • can-define
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
      • Pages
        • Magic Tag Types
        • Scope and Context
        • Expressions
        • Template Acquisition
        • Helpers
        • Live Binding
      • Methods
        • from
        • registerConverter
        • registerHelper
        • registerPartial
        • registerSimpleHelper
        • safeString
      • Tags
        • {{expression}}
        • {{{expression}}}
        • {{#expression}}
        • {{/expression}}
        • {{^expression}}
        • {{>key}}
        • {{!expression}}
        • {{else}}
      • Expressions
        • Bracket Expression
        • Call Expression
        • Hash Expression
        • Helper Expression
        • KeyLookup Expression
        • Literal Expression
      • Key Operators
        • @at
        • ~compute
        • ./current
        • ../parent
        • %special
        • this
        • *variable
        • key
      • Helpers
        • {{#if expression}}
        • {{#unless expression}}
        • {{#each expression}}
        • {{#with expression}}
        • {{log}}
        • {{#is expressions}}
        • {{#switch expression}}
        • {{#case expression}}
        • {{#default}}
        • {{joinBase expressions}}
      • Types
        • getterSetter
        • helper
        • helperOptions
        • renderer
        • sectionRenderer
        • simpleHelper
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-stache
  • /
  • sectionRenderer
  • / On this page
    • sectionRenderer

      typedef

      Renders a section. These functions are usually provided as .fn and .inverse on a stache helper's options.

      • source

      function(context, helpers)

      Parameters

      1. context {*|can-view-scope}:

        Specifies the data the section is rendered with. If a can-view-scope is provided, that scope is used to render the section. If anything else is provided, it is used to create a new scope object with the current scope as it's parent. If nothing is provided, the current scope is used to render the section.

      2. helpers {*|Options}:

        Specifies the helpers the section is rendered with. If a Options is provided, that scope is used to render the section. If anything else is provided, it is used to create a new scope object with the current helper scope as it's parent. If nothing is provided, the current helper scope is used to render the section.

      Returns

      {documentFragment|String}:

      Returns the rendered result of the helper. If the section is within a tag, like:

      <h1 {{#helper}}class='power'{{/helper}}>
      

      a String is returned.

      If the section is outside a tag like:

      <div> {{#helper}}<h2>Tasks</h2>{{/helper}} </div>
      

      a documentFragment is returned.

      Use

      Renderer functions are provided to stache helpers on the options argument and are used to render the content between sections. The context and helpers option let you control the data and helpers used to render the section.

      The following example adds {first: "Justin"} to the lookup data. Notice how the section has access to first and last.

      stache.registerHelper("myhelper", function(options){
        var section = options.fn({first: "Justin"});
        return $("<h1>").append( section );
      })
      
      var template = stache(
        "{{#helper}}{{first}} {{last}}{{/helper}}");
        
      template({last: "Meyer"}) //-> <h1>Justin Meyer</h1>
      

      If no context is provided, the current context is passed. Notice how the section has access to last:

      stache.registerHelper("myhelper", function(options){
        
         var section = options.fn();
         return $("<h1>").append( section );
        
      });
      
      var template = stache(
        "{{#helper}}{{first}} {{last}}{{/helper}}");
        
      template({last: "Meyer"}) //-> <h1> Meyer</h1>
      

      If a can-view-scope is provided, it is used to render the section. Notice how last is not available in the section:

      stache.registerHelper("myhelper", function(options){
        
        var section = options.fn( new Scope( {first: "Justin"}) );
        return $("<h1>").append( section );
        
      });
      
      var template = stache(
        "{{#helper}}{{first}} {{last}}{{/helper}}");
        
      template({last: "Meyer"}) //-> <h1>Justin </h1>
      

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.