DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
        • static
          • extend
        • prototype
          • ViewModel
          • events
          • helpers
          • leakScope
          • tag
          • view
          • viewModel
        • elements
          • <can-slot>
          • <can-template>
          • <content>
        • special events
          • beforeremove
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

<can-slot>

  • Edit on GitHub

Position the content of elements.

<can-slot name='NAME' BINDING>DEFAULT_CONTENT</can-slot>

Replaces any <can-slot name='NAME' /> element found in a component's view with the rendered contents of the <can-template /> element from the LIGHT_DOM that has a matching [TEMPLATE_NAME] attribute. Uses the scope of the LIGHT_DOM by default.

Component.extend({
    tag : 'my-email',
    view : stache(
        '<can-slot name="subject" />'
    )
});

var renderer = stache(
    '<my-email>' +
        '<can-template name="subject">' +
            '{{subject}}' +
        '</can-template>' +
    '</my-email>'
);

renderer({
    subject: 'Hello World'
});
//-> <my-email>Hello World</my-email>

Parameters

  1. NAME {String}:

    The name of the <can-template> to render in place of the <can-slot>.

  2. BINDING {can-stache-bindings}:

    You can bind to the context (also known as this) of of the corresponding <can-template>. This lets you pass data to the template. The following passes user as this to the corresponding <can-template name="user-details">:

    <can-slot name="user-details" this:from="user">
    

    toChild:from, toParent:to and twoWay:bind with this all work.

  3. DEFAULT_CONTENT {sectionRenderer(context, helpers)}:

    The content that should be used if there is no content in the matching <can-template>.

Use

To use <can-slot> we can create a Component that has <can-slot> elements in it's view and render that component with <can-template> elements in the LIGHT_DOM.

Any <can-slot> that has a name attribute matching the name attribute of a <can-template> will be replaced by the rendered inner contents of the <can-template>.

Component.extend({
    tag : 'my-email',
    view : stache(
        '<can-slot name="subject" />' +
        '<p>My Email</p>' +
        '<can-slot name="body" />'
    )
});

var renderer = stache(
    '<my-email>' +
        '<can-template name="subject">' +
            '<h1>{{subject}}</h1>' +
        '</can-template>' +
        '<can-template name="body">' +
            '<span>{{body}}</span>' +
        '</can-template>' +
    '</my-email>'
);

renderer({
    subject: 'Hello World',
    body: 'The email body'
});

/*
<my-email>
    <h1>Hello World</h1>
    <p>My Email</p>
    <span>The email body</span>
</my-email>
*/

Passing context

Context (this) can be bound to and passed to a template. The following passes <my-email>'s subject and body to the subject and body templates. Notice how subject and body are read by {{this}}.

var ViewModel = DefineMap.extend({
    subject: {
        value:"Hello World"
    },
    body: {
        value: "Later Gator"
    }
});

Component.extend({
    tag : 'my-email',
    view : stache(
        '<can-slot name="subject" this:from="subject"/>' +
        '<can-slot name="body" this:from="body"/>'
    ),
    ViewModel
});

var renderer = stache(
    '<my-email>' +
        '<can-template name="subject">' +
            '<h1>{{this}}</h1>' +
        '</can-template>' +
        '<can-template name="body">' +
            '<p>{{this}}</p>' +
        '</can-template>' +
    '</my-email>'
);

var testView = renderer({
    subject: 'Hello World',
    body: 'This is a greeting.'
});

/*
<my-email>
    <h1>Hello World</h1>
    <p>This is a greeting.</p>
</my-email>
*/

Default content

Default content can be specified to be used if there is no matching <can-template> or the matching <can-template> has no inner content.

Component.extend({
    tag : 'my-email',
    view : stache(
        '<can-slot name="subject">' +
            '<p>This is the default {{subject}}</p>' +
        '</can-slot>'
    )
});

var renderer = stache(
    '<my-email>' +
        '<can-template name="subject" />' +
    '</my-email>'
);

var testView = renderer({
    subject: 'content'
});

/*
<my-email>
    <p>This is the default content</p>
</my-email>
*/

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

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news