Templates 2
Content editing features
CMSity bundle comes with ready to use and customize _.edit.php, _.create.php, _.delete.php template files, and actions.php that is a template chunk responsible for rendering some action buttons.
As it was said before, CMSity has no back-office. However content editing templates and actions are really enough to easily create and modify existing content.
Edit template _.edit.php uses three special functions to achieve editing functionality, a_edit, a_new alternatively when creating new node, and p_field.
Function p_field is usable in generation of HTML code of the edit forms. Simple edit form may look as below:
<form method="post" action="<? p_path() ?>">
<? p_field('title', 'Title') ?>
<? p_field('content', 'Content') ?> </form>
Action function a_edit, called at the beginning of edit template, checks whether there are some values posted via POST and allowed to be modified and modifies current node's content.
Template _.edit.php shipped with CMSity bundle may look obscure at the first look, however most of the code at the beginning is preparing a_edit parameters (a_new alternatively) checking logged user tags and current node's type. This makes using single _.edit.php as a template for editing all node types for sample CMSity site, shipped with the bundle.
Function a_edit has two objectives, (1) validate POST data relatively to constraints passed as parameters to the function and (2) modify the current's node content is the validation is successful.
1st a_edit parameter is an array of fields allowed to be modified that will come inside POST, 2nd a_edit parameter is an array or fields required to be present in POST with non-empty values. To explain this more, imagine we want to create template for an action change-title, that will allow editing node's "title" for all logged users that are "Titlemator".
_.change-title.php
<? if(t_is('titlemator')) a_render(403) ?>
<? include('header.php') ?>
<? if(a_edit(array('title'), array('title')) === true): ?>
<h1>Title has been changed</h1>
<? endif ?>
<form method="post" action="<? p_path() ?>" id="edit_form" enctype="multipart/form-data">
<? p_field('title', 'Title') ?>
</form>
<? include('footer.php') ?>Analizing sample above line by line:
- checks if current user is "Titlemator" (note it uses a name/slug of the term not the term's title, thats why for "Great Mage" we shall type 'great-mage') and if the user does not match it renders 403 error.
- includes a header template chunk (for flexibility)
- launches a_edit having "title" as allowed and required parameter.
- displays a message is a_edit returns TRUE (which means that modification was done successfully)
- ...
- defines a form that submits to p_path returning current node URL
- outputs HTML form field having a name "title" and label "Title"
- closes form
- includes a footer template chunk
As a conclusion a_edit and a_new are API functions that modify and create new nodes, a_delete deletes current node.
It is important to remember checking logged user tags prior calling these in order to maintain access right.
Smilies, HTML escaping... special p parameters
Function p offers extra 4th parameter responsible for special behavior (see function documentation) when rendering value of the node's property. Some of them that are worth to mention are:
- If you want to replace all textual smile entries ;) :P with images use O_SMILIES, it will take rules configured in config.php to replace smilies with <img> tags.
- O_ESCAPE_ALL and O_ESCAPE_CONFIG will escape all or configured HTML tags, this is useful for protecting content generated from site users against site's HTML breaches. Every time when rendering "content" of some forum's "topic" or blog "reply" it is worth to use <? p('content', false, false O_ESCAPE_CONFIG) ?>
- Normally when value is empty p doesn't print anything and skips requested tag generation. O_PRINT_EMPTY makes p print a dash ("-") wrapped by tags as requested, so <? p('author', 'td', false, O_PRINT_EMPTY) ?> will output <td>-</td> on empty value rather than absolutely nothing.
