-
- 8 Posts
Hello,
I've been learning during the last weeks on how to use modx by building a test website and find it very interesting.
I have a debate goind in my mind right now about chunk templating and would like some input from other experienced modx users.
I am trying to decide which way is more efficient to handle dynamic content:
1. Use chunks and templating. The dynamic HTML is generated by a snippet.
2. Use chunks with javascript. The dynamic HTML is generated by a javascript function.
For example, to create a dynamic table. I could:
1. Create a chunk for the rows. Then in a php script (snippet) get the data and generate the output.
2. Get the data from the database, send it to the chunk and have a javascript function generate the table.
The reason why I'm not conviced with the first method is that I would have to create many chunks. With the javascript method I only have 1 chunk. Also, I think that the second method makes things easier for AJAX.
Am I correct in my thinking? What do you people think?
Thank you
If your table doesn't have thousands of rows, I'd rather using snippet.
If it does, I'd rather using AJAX using REST method (depends on the JS framework, the REST would work for the scrolling or pagination).
This option also skips chunk, you could depend on the JS's templating system.
Rico
Genius is one percent inspiration and ninety-nine percent perspiration.
Thomas A. Edison
MODx is great, but knowing how to use it well makes it perfect!
www.virtudraft.com
Security, security, security! |
Indonesian MODx Forum |
MODx Revo's cheatsheets |
MODx Evo's cheatsheets
Author of
Easy 2 Gallery 1.4.x,
PHPTidy,
spieFeed,
FileDownload R,
Upload To Users CMP,
Inherit Template TV,
LexRating,
ExerPlan,
Lingua,
virtuNewsletter,
Grid Class Key,
SmartTag,
prevNext
Maintainter/contributor of
Babel
Because it's hard to follow all topics on the forum, PING ME ON TWITTER
@_goldsky if you need my help.
-
- 24,544 Posts
JavaScript can be pretty slow. Usually, the fastest method is to use a chunk with placeholder tags. You put the values for the placeholders into an array, then use $modx->getChunk() and return the result.
$fields = array(
'placeholder1' => $value1,
'placeholder2' => $value2,
);
return = $modx->getChunk('ChunkName', $fields);
You can make it a little bit faster by using str_replace() to replace the placeholders, but the method above is usually fast enough.
-
- 149 Posts
Don't forget about search engines and noscript users, they don't like javascript-only (without noscript fallback) websites ))
-
- 8 Posts
Thanks for the input.
It seems that chunks is the way to go. There is one small issue that still bothers me when using chunks. I'm trying to use them in the most pure way, taking out all html from the snippets. But when using a <table> I cannot do this totally.
I would need to have in my snippet something like:
$output = $modx->getChunk('ChunkName', $fields);
return '<table><tbody>'.$output.'</tbody></table>'
The only way to remove this html would be to create 2 more chunks, one for "table start" and another for "table end".
Is there a better way to do this?
-
- 5,888 Posts
-
- 24,544 Posts
The usual solution is to have two or three Tpl Chunks. This example is from my book. It shows one way to do it with a list, but I think you'll get the idea:
<!--ItemTpl Code -->
<span class= "sl.item_class">[[+sl.item]]</span>
<!--RowTpl Code -->
<li class="sl.row_class">[[+sl.row]]</li>
<!-- OuterTpl Code -->
<div class="sl.show_list ">
<ul class="sl.outer_class">
[[+sl.outer]]
</ul>
</div>