Snippet gets the chunk contents by its name. For example, it useful to get chunks inside JS code.
Also it can:
- Pass additional data to chunk for parsing.
- Escape special chars for JS.
- Remove empty placeholders.
- PHP >= 5.6
- (MODX)EvolutionCMS >= 1.1
- (MODX)EvolutionCMS.libraries.ddTools >= 0.60
Just run the following PHP code in your sources or Console:
//Include (MODX)EvolutionCMS.libraries.ddInstaller
require_once(
$modx->getConfig('base_path') .
'assets/libs/ddInstaller/require.php'
);
//Install (MODX)EvolutionCMS.snippets.ddGetChunk
\DDInstaller::install([
'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddGetChunk',
'type' => 'snippet'
]);- If
ddGetChunkis not exist on your site,ddInstallerwill just install it. - If
ddGetChunkis already exist on your site,ddInstallerwill check it version and update it if needed.
- Snippet name:
ddGetChunk. - Description:
<b>2.4</b> Snippet gets the chunk contents by its name. For example, it useful to get chunks inside JS code.. - Category:
Core. - Parse DocBlock:
no. - Snippet code (php): Insert content of the
ddGetChunk_snippet.phpfile from the archive.
- Create a new folder
assets/snippets/ddGetChunk/. - Extract the archive to the folder (except
ddGetChunk_snippet.php).
-
name- Desctription: Chunk name or code via
@CODE:prefix. - Valid values:
stringChunkNamestring— use inline templates starting with@CODE:
- Required
- Desctription: Chunk name or code via
-
placeholders- Desctription:
Additional data has to be passed into the chunk.
Arrays are supported too:some[a]=one&some[b]=two=>[+some.a+],[+some.b+];some[]=one&some[]=two=>[+some.0+],[some.1]. - Valid values:
stringJsonObject— as JSONstringHjsonObject— as HJSONstringQueryFormatted— as Query string- It can also be set as native PHP object or array (e. g. for calls through
\DDTools\Snippet::runSnippetor$modx->runSnippet):arrayAssociativeobject
- Default value: —
- Desctription:
Additional data has to be passed into the chunk.
-
removeEmptyPlaceholders- Desctription: Placeholders which have not values to be replaced by will be deleted from parsed chunk if the parameter equals 1.
- Valid values:
01
- Default value:
0
-
escapeResultForJS- Desctription: Escaping special chars for JS.
- Valid values:
01
- Default value:
0
All examples are written using HJSON for the placeholders parameter, but if you want you can use vanilla JSON instead.
<script>
var form = '[[ddGetChunk? &name=`someForm` &escapeResultForJS=`1`]]';
//Insert form to page
$('body').append(form);
</script>[[ddGetChunk?
&name=`someChunk`
&placeholders=`{
id: 33
orderBy: "someTv ASC, pub_date DESC"
}`
]]
Code of someChunk:
<div class="some">
<div class="someDesignDiv"></div>
<div>
[[Ditto?
&startID=`[+id+]`
&orderBy=`[+orderBy+]`
&tpl=`someChunk_item`
]]
</div>
</div>\DDTools\Snippet::runSnippet([
'name' => 'ddGetChunk',
'params' => [
'name' => 'someChunk'
]
]);