Don’t know if this will be useful to anyone, but here’s an example of how I’ve been using the modx API, template variables and Chunks to save time in a recent project. Being able to be this flexible is what I love about MODx
Lots of projects involve data related to a single record.
eg Name, Phone Number etc.
There are a number of ways that this can be handled, for small amounts of data, I like to use tv’s, as its easy, and you can have end users add content to the pages reasonably easily.
(Larger amounts of data, I prefer to use external tables - See Ganesh’s post for a howto on that using phpEdit).
In this example, I needed to store details about stores in a shopping mall.
Each store has various fields - phonenumber, location, photo, shop name etc.
First I created a bunch of TV’s for the different attributes for the store.
I then created a new folder called Stores, then for each store entered the data for the store in the TV’s for each page under that folder.
eg
Store -
- Store xxx ... field contents in TV’s .
- Store yyy ... field contents in TV’s .
I created a chunk called tvShopDetails which displays the tv’s in a nice way for each page, and call that in the content of each store page to display.
The display chunk - tvShopDetails looks like this, and lists the fields (tv’s) I’m using:
<div id="store_title">
<div id="store_title_l">[*tvShopTitle*]</div>
<div id="store_title_r">[*tvShopNo*]</div>
</div>
<div id="store_img">[*tvShopPhoto*]</div>
<div id="store_details">
<div id="store_title_l">Brands: [*tvShopBrands*]<br />
Store nr: [*tvShopStorenr*]<br />
Level: [*tvShopLevel*]<br />
Phone: [*tvShopPhone*]<br />
</div>
<div id="store_title_r"><a href="#" onClick="Lightbox.showBoxByAJAX('/location.php?myNumber=[*tvShopNumber*]&myX=[*tvShopX*]&myY=[*tvShopY*]&myLevel=[*tvShopLevel*]', 840 ,570);return false;">location</a></div>
</div>
I just call the chunk from the page itself using
{{tvShopDetails}} as the contents of each store page I create.
My problem was how to use the chunk programmatically for the store, as I wanted to show the data automatically after choosing it from a dropdown. After delving into the API, its a piece of cake.
To create a dropdown, I created a chunk called DoDropDown which called Ditto, with a simple template for listing everything in the Store folder.
{{DoDropDown}}
<form method="get" action="" onsubmit="return false;" >
<select name="store_id" id="store_id" onchange="this.form.submit();" class="popup" >
<option selected="true" >- ALPHABETICAL ORDER -</option>
[[Ditto? &tpl=`chkAlphabetical` &startID=`154` &display=`all` &sortDir=`ASC` &sortBy=`pagetitle`]]
</select>
[[GetStoreID]]
Its a fairly straightforward Ditto call (the Store folder is 154), which just lists everything in the store folder.
The Ditto Template (chkAlphabetical) for the dropdown is simple, and just contains
<option value="[+id+]">[+title+]</option>
GetStoreID is a small snippet I made which I pass the ID of the page containing the store data.
<?php
$id=0;
$id = $_GET ['store_id'];
if ($id=="" || $id==0 ) return; //rudimentary troubleshooting
$fields= array ("title","id","tvShopX", "tvShopY", "tvShopLevel", "tvShopBrands", "tvShopNumber", "tvShopPhone", "tvShopPhoto", "tvShopTitle");
$fields=$modx->getTemplateVarOutput($fields,$id);
$content = $modx->parseChunk('tvShopDetails', $fields,'[*', '*]');
return $content;
?>
The magic is in the API calls.
In the above snippet, I create an array of the TV’s I want. Then tell MODx to go get me my TV Data, and put it into the fields array.
Once its there, I use the ParseChunk function to have MODx do all the heavy lifting so I can use the layout chunk I created.
Don’t know if this will be useful to anyone else, but I haven’t seen many examples of how to do this kind of thing here, so I thought I’d share.
An example of the working version is here -
http://foxtown.computersolutions.cn/DDT
(DDT = Drop Down Test)
Comments welcome - this is kind of my first attempt at explaining how I do stuff like this in MODx!
Lawrence /
http://www.computersolutions.cn
(Edited to correct a typo on the chunk name, and to clarify use of tv’s slightly)