Skip to content

Instantly share code, notes, and snippets.

@harleyholt
Created October 20, 2011 22:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harleyholt/1302572 to your computer and use it in GitHub Desktop.
Save harleyholt/1302572 to your computer and use it in GitHub Desktop.
Example of Knockout Conditionally Rendered Template
<!-- Example one shows the most verbose way of specifying a template that is only rendered if someObject is defined in the current view model
This information was culled from https://github.com/SteveSanderson/knockout/blob/1.3/src/binding/defaultBindings.js#L445
-->
<script type="text/html" id="sub-example-one">
<h2 data-bind="text: title"></h2>
<p data-bine="text: description"></p>
</script>
<script type="text/html" id="example-one">
<!-- the 'if' binding says that this template will only be rendered if it's argument is truthy -->
<div data-bind="template: { name: 'sub-example-one', if: exampleObject, data: exampleObject }">
</div>
</script>
<!-- Example two is equivalent but uses an inline template -->
<script type="text/html" id="example-two">
<div data-bind="template: { if: exampleObject, data: exampleObject }">
<h2 data-bind="text: title"></h2>
<p data-bine="text: description"></p>
</div>
</script>
<!-- Example three is equivelent but using the 'with' binding as a shortcut
for the template with an 'if' and 'data' argument -->
<script type="text/html" id="example-three">
<div data-bind="with: expectedObject">
<h2 data-bind="text: title"></h2>
<p data-bind="text: description"></p>
</div>
</script>
@SwethaG
Copy link

SwethaG commented Jun 25, 2019

Hi,

I need to use parent data in if condition. I am using as below but it is not working. Can I use like below? Please let me know
div class="row-fluid" data-bind="template: { name: TemplateName, if: $data.GrpGid == $parent.id, data: PrdctParmValue }">

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