This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi This is probably staring me in the face but I can't figure out how to do it. I've created a snippet in the Static Pages plugin from a partial. The snippet requires a property of which I need the value in the code section of the partial. I manage to read the property in Twig in the markup section but I can't figure out how to access it in the code section. Can I get it in the onStart() function and how do I access it? Thanks!
Last updated
I've down it :) Simply use the code. I've done this to get one single employee from an id-number. Instead of ":id" you do "id":
description = "Anställda"
[viewBag]
snippetCode = "employees"
snippetName = "Anställda"
snippetProperties[id][title] = "Id-nummer"
snippetProperties[id][type] = "string"
snippetProperties[id][default] = 0
snippetProperties[id][options][] = ""
[Employees]
itemId = "{{ id }}"
maxItems = 36
orderBy = "id"
sort = "desc"
page = 1
perPage = 12
filterRole = 0
==
{% component 'Employees' %}
Apparently, there is a variable $this->viewBag, accessible from the code section. However, it seems to be always empty, from what I've tried (athough I have entered property values for that snippet):
public onStart() {
echo "<h3>Viewbag properties</h3>";
foreach ($this->viewBag as $key => $prop) {
echo $key ." => " . $prop;
}
public onEnd() {
echo "<h3>Viewbag properties</h3>";
foreach ($this->viewBag as $key => $prop) {
echo $key ." => " . $prop;
}
Last updated
Oh, apparently snippet variables (just like variables passed down as arguments to partials, i.e. like {% partial 'whatever' thisHereVariableBeLike="someValue" %}
) are simply stored in $this
in the code section's context.
So, for example if you have a snippet partial like this:
description = "whatever"
[viewBag]
snippetProperties[whatever][title] = "someSnippetProperty"
snippetProperties[whatever][type] = "text"
==
<?php
// Code section
function onStart() {
echo $this->whatever = $this->someSnippetProperty;
}
?>
==
{# HTML section #}
{# 'this.someSnippetProperty' was set as a snippet property. #}
<h3>{{ this.someSnippetProperty }}</h3>
{# 'this.whatever' was assigned the value of 'this.someSnippetProperty' in the code section. #}
<h3>{{ this.whatever }}</h3>
1-6 of 6