Documentation

Light-weight, fast, simple and powerful!
 
Translations of this page?:

Wolf CMS Functions Reference Book

Welcome to the Wolf CMS Functions Reference Book. This book is a collection of wiki pages from the wolfcms.org documentation wiki that are gathered together to form a book-like total.

By accessing this book and selecting the “Export to ODT” button, you can generate an Open Office document for your viewing/printing pleasure. You can of course also use Open Office to turn the ODT document into a PDF file.

author()

Wolf saves the id of the user who creates a page, and the name for that id can be displayed using author(). This is commonly given in the page's “meta”, for example:

<p class="meta">Posted by <?php echo $this->author(); ?></p>

See also the updater() function.

authorId()

Wolf saves the unique id number of the user who creates a page, and that id number can be accessed using authorId(). This might be especially useful if two “authors” registered on the system have the same name. For example:

<?php if ($this->authorId() == '1') {
  // do things
  } else {
  // do something else
  }
?>

would be a more reliable test than one using the author() function (which returns the name) if two users were both registered with same name.

There is also a updaterId() function available.

breadcrumb()

When a page is created in Wolf, the “Breadcrumb” value is automatically completed with the same values as the page title itself. This is the page reference used automatically by the breadcrumbs function, but it can also be used on its own:

<?php echo $this->breadcrumb(); ?>

The “Breadcrumb” field is found under the “Metadata” tab next to the “Page Title” tab, and the value can be edited.1) It may be, for example, that a very long page title could be abbreviated for use as a “breadcrumb”. Once edited, and different from the Page Title, its value will remain unchanged even if the Page Title itself is changed.

breadcrumbs()

“breadcrumbs” can be created in Wolf by adding this code to a Layout:

<?php echo $this->breadcrumbs(); ?>

By default, it produces output of this kind:

Home > Music > Baroque > Bach

In order to change the value of the separator, include it as a parameter. If for example, you wish to use a forward slash:

<?php echo $this->breadcrumbs('/'); ?>

Any character may be used this way.

Note: if you wish to use a backslash, it must be given twice, since it is a PHP escape character: $this->breadcrumbs('\\').

For more information on the titles of the breadcrumb links themselves, see the breadcrumb function entry.

children()

children() returns an array of values relating to the child pages of the current page.2) Normally, then, it is not used on its own, but to give some information about published subpages to a given page.

The array produced by children() requires a foreach loop to present usable information. The most simple listing of subpage titles, then, could look like this:

<h3>List of pages</h3>
 
<ul>
    <?php  foreach ($this->children() as $child) : ?>
    <li><?php echo $child->title(); ?></li>
    <?php endforeach; ?>
</ul>

In situations when in return only a single result is desired, the foreach loop should be ditched in favor of limit ⇒ 1 argument (see Arguments below in this page), otherwise it will not work and a fatal error is returned instead. The example below returns the last published page from Articles as the parent page.

<?php 
  $page_article = $this->find('/articles/');
  $last_article = $page_article->children(array('limit'=>1, 'order'=>'page.created_on DESC')); ?>
    <h2 class="post_title"><?php echo $last_article->link(); ?></h2>
    <?php echo $last_article->content(); ?>
    <?php if ($last_article->hasContent('extended')) echo $last_article->link('Continue Reading&#8230;'); ?>

For further information on how to use children() in constructing menus, see how to Display a list of subpages.

Conditions

Including hidden pages

By default, children() only returns “published” pages.3) In the following line of code, the final 'true' tells Wolf to include hidden pages as well:

$this->children(null,array(),true)

Additional arguments

Four more arguments can be given to children() to further define the subpages it returns:

  • where - sets a condition
  • order - determines the sort order (by field name in page table [see note below], either ASC ascending, or DESC descending)
  • offset - where in the list of subpages to begin the list
  • limit - how many pages to return

A note on “order”

Any of the fields in the page table can be used to sort your “children” pages. In first example, below, you could have:

'order' => 'title ASC'

to arrange the order by the page Title in A-Z order, or

'order' => 'slug DESC'

to order the list by “slug” value in Z-A order. That should give you the idea! While any value in the “page” table could be used here, the main options would include:

  • title
  • slug
  • breadcrumb
  • created_on
  • published_on
  • updated_on
  • created_by_id
  • updated_by_id

The default is position, which is set automatically when the drag-drop page re-ordering is used.

Examples

Example 1: Last five pages

<?php $last_result = $this->children(array('limit' => 5, 'order' => 'created_on DESC')); ?>

Then you need to use $last_result instead of $this->children() in the foreach loop described above to produce the list. Adjusting the simple code above for listing the titles of the last five pages would look like this:

<h3>Last five pages</h3>
 
<ul>
    <?php $last_result = $this->children(array('limit' => 5, 'order' => 'created_on DESC')); ?>
    <?php  foreach ($last_result as $child) : ?>
    <li><?php echo $child->title(); ?></li>
    <?php endforeach; ?>
</ul>

Example 2: Only certain pages

If you only wanted to list results 3 and 4, for example, ordered by the position (by default), you would use:

<?php $last_result = $this->children(array('offset' => 2, 'limit' => 2)); ?>

Since PHP starts counting at “0”, the offset needs to starts at 0 not at 1. Thus setting the offset at “2” actually gives you the third page. Again, as in the previous example, $last_result is used in the foreach loop to produce the desired list.

Example 3: Order hidden articles by published date

To return a list of 10 hidden articles by published date for example, use the following:

<?php $last_result = $this->find('/articles/')->children(array('order'=>'published_on DESC','limit'=>10),array(),true); ?>

childrenCount()

The childrenCount() function returns a count of how many child pages belong to the current page. A simple echo $this->childrenCount(); will return the number of “published” pages to the current page.4)

childrenCount() can be useful, then, for determining when to include navigation, for example:

<?php if ($this->childrenCount() > 0) {
  // if count is > 0, there are child pages, so do stuff
  } else {
  // no child pages, so do something else
  }
?>

It takes the same parameters as the children() function; see the documentation there for details.

Constants

Wolf uses some “constants” which can be useful for site development:5)

URL_PUBLIC

URL_PUBLIC returns the homepage URL of the Wolf website in the format http://www.mywolfsite.com/ (with trailing slash). It is used throughout the default Layouts to provide a dynamic reference to the website. For example, the favicon is called in the Simple layout with this code:

  <link rel="favourites icon" href="<?php echo URL_PUBLIC; ?>favicon.ico" />

URI_PUBLIC

URI_PUBLIC returns the home location of the Wolf website. This makes it possible to pass a reference relative to the location of Wolf's index page. If Wolf's index.php lives in the root directory (e.g., http://www.mywolfsite.com/), then echo URI_PUBLIC will simply return /. If it lives in a subdirectory (e.g. http://www.example.com/wolfcms/), then echo URI_PUBLIC will return the root directory like this: /wolfcms/

BASE_URL

BASE_URL is closely related to URL_PUBLIC, and when USE_MOD_REWRITE is set to “true” in config.php, they are identical. However, when USE_MOD_REWRITE is set to “false”, BASE_URL will include the ? in the URI.

In the default sidebar to the Home Page, the RSS feed link is given this way:

  <a href="<?php echo BASE_URL; ?>rss.xml">Articles RSS Feed</a>

CURRENT_URI

Echoing CURRENT_URI will return the part of the current URL following the URI_PUBLIC (see above), ignoring any page suffixes. Examples:

Full URL URI_PUBLIC CURRENT_URI
http://www.mywolfsite.com/about_us / about_us
http://www.example.com/wolf/about_us /wolf/ about_us
http://www.example.com/wolf/about_us.html /wolf/ about_us
http://www.example.com/grocers-site/fruit/oranges.html /grocers-site/ fruit/oranges
http://www.mywolfsite.com/articles/2009/07/24/my_second_article / articles/2009/07/24/my_second_article

CMS_ROOT

The CMS_ROOT contstant returns the file path of the server to the directory where Wolf is installed. If you installed Wolf to the htdocs root directory of your site, this code:

<?php echo CMS_ROOT; ?>

would return a value something like:

/home/vol8/mywolfsite.com/my_account_name/htdocs

content() function

As its name suggests, the content() function returns the content of pages created in Wolf. More specifically, it returns the content of the page-part given as a parameter; if no parameter is given, then it defaults to the body page-part, circled in red in this graphic:

Page-part tabs

In order to display the body, put this code at the appropriate place in your layout:

<?php echo $this->content(); ?>

Other content parts can be created by clicking the green “+” icon above the upper-right corner of the page editing box. (The red “-” icon deletes the active part/tab, so be careful!)

Page-part (tab) creation button

If you need to display one of these “custom” parts, like the sidebar tab in the Home Page graphic above (circled in blue), just add it to the parameter:

<?php echo $this->content('sidebar'); ?>

Note: if you use <?php echo $this->content(); ?> in page content, it will create an infinite loop: so don’t do it! In other words, only use that code for the page body tab in a Layout, not in a Page.)

"Inheriting" content

In order for the “part” to be inherited by “child” pages (and “child-of-child” pages, etc.), use the true parameter:

<?php $this->content('sidebar', true); ?>

Note: if the current page has its own equivalent “sidebar” part, then that will prevent the “inheritance” from the parent page from taking place.

Displaying the content of one page on a different page

If you want to call the content from a some page onto a different page, you must use the find() function in conjunction with content(). For example, to display the content of the default “About us” page on any other page, use this code:

<?php echo $this->find('about_us')->content(); ?>

date()

To display the date a page was created, use:

<?php echo $this->date(); ?>

If you want to change the format of the date, you can pass it as the first parameter of the method like this:

<?php echo $this->date('%A, %e %B %Y'); ?>

For more information about the date format, check the PHP manual for strftime.

On Windows, and rarely in other settings, the use of %e may prevent any date appearing! In this case, use %d in its place. (See PHP Bugs for more information, the date howto for a work-around.)

Values

The default date returned is the page’s creation date. The dates which can be displayed are:

  • created — (default) which returns the date the page was initially stored in the database, no matter what “Status” it had;
  • published — which returns the date the page was first saved with the “Status” set to “Published”; and
  • updated — which returns the most recent date on which that page was altered (this can include re-ordering)

Example

For example, if you want to display the last updated time of this page, use this:

<?php echo $this->date('%a, %e %b %Y', 'updated'); ?>

How to translate every date in your layout

Set the locale to your language with the setlocale PHP function:

<?php setlocale(LC_ALL, 'fr_CA.utf8'); ?>

For more information about this function, consult the PHP manual on setlocale.

description()

In editing a page, under the Metadata tab is the “Description” field which makes use of the description function. The default Layout includes this line in the <head>…</head> section:

  <meta name="description" content="<?php echo ($this->description() != '') ? $this->description() : 'Default description goes here'; ?>" />

This checks to see if there is any Metadata filled in for the page, and if there is, it is used. Otherwise, the default text is used.

executionTime()

The executionTime() function returns the time in seconds it takes for the page to be rendered. It can be called this way:

<?php echo $this->executionTime(); ?>

When 'DEBUG' is defined as “true” in config.php, this information is also reported in the footer of the admin pages.

find(uri)

The find() function can be used to find and retrieve Page objects. As such, it needs a value to search on, either a “slug” value or a variable. It returns a Page object which has further functions to retrieve page information (or an array of information, depending on which of the object's function you use). A simple example:

<?php
    $pageobject = $this->find('/about-us');
    echo $pageobject->link();
?>

You can also directly access the object's (in this case) link() function with a short hand notation. This prevents you from first having to assign the result of the find() function to a variable before accessing the object's functions. A simple example:

<?php echo $this->find('/about-us')->link(); ?>

These two examples are functionally the same and produce the following HTML from anywhere in the site:

<a href="http://www.mywolfsite.com/about_us">About us</a>

For the “main” level of navigation, find() does not need forward-slashes. All of these will produce the same result:

  • $this->find('/about-us')
  • $this->find('/about-us/')
  • $this->find('about-us')

find() can be used in conjunction with most (probably all!) of Wolf's other functions, not just “link”, as in the example above.

See also the entry for the findById() function.

Examples

"Finding" pages at Level 2

When looking for child-of-child pages6), both terms need to be given:

  • “hard” set: $this->find('fruit/apples');
  • using variables: $this->find($parent.'/'.$subpage);

Note that the getUri() function gives all slugs for a page, including the slugs of all ancestor pages.

Using variables

The value of the search term can be contained in a variable. For example, the conditional navigation in the “Wolf” default layout finds a variable, $parent, which is the current top-level navigation page. Using this value in conjunction with the "children" function produces a dynamic listing of child-pages, giving a simple menu:

<?php // simplified code:
    $topPage = $this->find($parent);
?>
 
<ul>
<?php foreach ($topPage->children() as $subPage) : ?>
    <li><?php echo $subPage->link(); ?></li>
<?php endforeach; ?>
</ul>

getUri()

This function will return the "slug" values which point to a given page.

Notes

For this code: <?php echo $this->getUri(); ?>, note the different results:

  1. for URL: http://www.wolfsite.com/
    • getUri() = [nothing]
  2. for URL: http://www.mysite.com/wolf/ (when installing Wolf CMS in a subdirectory 'wolf')
    • getUri() = [nothing]
  3. for URL: http://www.wolfsite.com/about_us
    • getUri() = about_us
  4. for URL: http://www.wolfsite.com/about_us.html
    • getUri() = about_us
  5. for URL: http://www.wolfsite.com/articles/2009/11/10/my_first_article
    • getUri() = articles/my_first_article

Note in the last example that only the “slug” values are given, not the “yyyy/mm/dd” values generated by the Archive plugin. For this page behaviour, compare the related “constant”, CURRENT_URI.

Examples

Finding "top slug" for page tree

It is often useful to find the slug of the top (level 1) page in a tree. This can be using for conditional navigation, or setting a unique background or banner for that area of the site, etc. The most simple code for this can use the getUri() function:

// Returns the top parent slug:
$topParent = reset(explode('/', $this->getUri()));

For the URI of fruit/apples/granny-smith with the code above, then echo $topParent; would return fruit.

Get list of "sibling" pages

If one wanted a list of “sibling” pages (at same level, with same parent), you wouldn't know in advance how many slugs were needed in the find-> statement, so here again getUri() should be used. The following code (to be used in a Layout) produces a simple sibling list of this kind:

  • Sibling1
  • Sibling2
  • Sibling3
<?php if ($this->level() > 0) : ?>
<ul>
<?php foreach ($this->find($this->parent->getUri())->children() as $sibling) : ?>
    <?php if ($this->slug() != $sibling->slug()) : ?>
<li><?php echo $sibling->link(); ?></li>
    <?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Note that the code as given omits the current page. To include all sibling pages, including the current page, remove the “inner” if/endif statements (lines 4 and 6).

hasContent()

The hasContent() function may be thought of as the conditional counterpart of the content() function.7)

It can take two parameters:

  1. A page-part must given as the first parameter. If it is found, the function returns “true”, otherwise it returns “false”.
  2. Like content(), the hasContent() function can also be inherited by setting the second parameter to “true” (optional); by default this is set to “false”.

Examples

Again, like content(), this function relates to a given object, and so is used like this:

<?php echo $this->hasContent('page-part') ? $this->content('page-part'): ''; ?>

For the page-part to be inherited by all descendant pages, use:

<?php echo $this->hasContent('page-part') ? $this->content('page-part', true): ''; ?>

Version notes

< 0.6.0

In versions up to and including 0.6, the name does not quite match the operation of this function: it does not test to see if “page-part” has any content, and then echoes it if there is some text there. Rather, it checks to see if there is a “page-part”, and then echoes whatever is there.

In other words, this function tests to see if a part exists, and will return “true” even if the part is there with no content at all.

0.7.0+

With the introduction of the partExists function, hasContent() now behaves as expected by the name: it checks to see if the part exists and if that part contains any content.

id()

The id() function returns the database ID of the current page. This code:

<?php echo $this->id(); ?>

will return a number, e.g. “1” for the root page (“Home page”) created at install time.

Note that for Archive pages, (e.g., the %B %Y archive monthly archive page created during installation), the id() result will be the same no matter which month (in this example) is being displayed, since each one is the same “page”.

Finding the ID of a page

To discover the ID of any page (“68” in the examples below), either (a) hover over the title in the Pages index, and the page id and slug will be displayed in a tooltip:

Tooltip displays page ID and slug

or (b) click the “Settings” tab when editing the page:

Settings

See also

The following functions also make use of the id value of a page:

includeSnippet()

This function is used to include "snippets" in Wolf pages. The syntax is:

<?php $this->includeSnippet('the_name_of_the_snippet'); ?>

On the way the special PHP variable $this-> behaves when used in a snippet, see the documentation for $this->.

Snippets may call/include other snippets.

From 0.7.0:8) the includeSnippet() function will return “true” if the snippet name is found, but “false” if the snippet name does not exist. If a test is used which evaluates includeSnippet() as “true”, the value of the snippet will be passed automatically.

Hints

Conditional Use of Snippets

You could also create a snippet with your php code, then only include it on the relevant page by using a conditional statement. That way you won't need a page-part, and it's only a small addition to your layout:

<?php // only include on contact page or children
if(url_start_with('/contact'))  $this->includeSnippet('the_name_of_the_snippet'); ?>

Just keep in mind that it will also be included in any children pages, because it's looking for a url that begins with the provided text.

HT: mrgoatee

keywords()

In editing a page, under the Metadata tab is the “Keywords” field which makes use of the keywords function. The default Layout includes this line in the <head>…</head> section:

  <meta name="keywords" content="<?php echo ($this->keywords() != '') ? $this->keywords() : 'default, keywords, here'; ?>" />

This checks to see if there are any keywords filled in for the page, and if there are, they are used. Otherwise, the default text is used.

level()

level refers to the “distance” of a page in the tree from Home Page or, more precisely, the number of elements a given page is distant from the “root” in the URI. “Home Page” (or “root” in the URI) is at zero.

You can echo the level of a page to the screen using this code:

<?php echo $this->level(); ?>

Examples

In this URI: this page: is at this level:
http://www.wolfsite.com/ (home page) 0
http://www.site.com/wolf/ (home page) 0
http://www.wolfsite.com/about-us.html about-us.html 1
http://www.wolfsite.com/about-us/contact contact 2

It is important to note that the “archive” (“blog”) type pages work this way:

In this URI: this page: is at this level:
http://www.wolfsite.com/articles articles 1
http://www.wolfsite.com/articles/2009/03/07/my-news my-news 5
= http://www.wolfsite.com/articles/my-news my-news 5

In an “archive” setting, the level of my-news is always 5, even though it is the child of the articles page which is at level 1.

Usage note

level() is very useful in a test to keep things off the “Homepage” that should only appear “inside” the site:

<?php if ($this->level() != 0) : ?>
 
    ... do stuff inside the site, but not on the Homepage ...
 
<?php endif; ?>

The if() test checks to see if the level is not '0' (= homepage), and if not, whatever appears before the endif will be run.

Wolf provides a simple ”previous” and “next” function. next returns an array of values relating to the page following the current page, if it exists.10) It is thus not used on its own, but normally would be used in the Layout to provide a link to the “next” page.

Example

The following code tests to see if a “next” page exists. If it does, it makes a link accompanied by a label with a double-arrow pointing right:

<?php if ($next = $this->next()): ?>
  <div style="float: right; border-top: thin solid #ccc; padding-top: 4px;">Next &#187; <?php echo $next->link(); ?></div>
<?php endif; ?>

See also previous.

Page Status Definitions

Introduction

Wolf assigns one of four different “status” levels to pages; each status named below also has a corresponding numerical value. Note these two examples:

(1) This returns the numerical value corresponding to the page status:

echo $this->status_id;

(2) The name can be used when testing for a particular status:

if ($this->status_id == Page::STATUS_PUBLISHED) { echo 'PUBLISHED'; }

This is the equivalent of this code (which produces the same result):

if ($this->status_id == 100) { echo 'PUBLISHED'; }

N.b.: status_id does not take a parameter, and has no ”()” ending.

Draft

Constant: Page::STATUS_DRAFT

The “draft” status is for use during the early stages of producing a page, before it is ready for display on the frontend of the website.

  • it will NOT be listed by: $this->children()
  • it will NOT be found by: $this->find('its_uri')
  • it is NOT possible to access the page directly with its full url
  • $this->status_id would return a value of 1 (but note draft pages do not appear on the front end!)

Preview

Constant: Page::STATUS_PREVIEW

This status allows content editors to view a page “live” before publishing it; it can only be accessed by full URL (or the “View this page” link while editing in the backend) by a content editor who is logged in to Wolf.

  • it will NOT be listed by $this->children()
  • it will NOT be found by $this->find('its_uri')
  • it is possible to access it directly with its full url ONLY IF logged in with the role of administrator, developer, or editor
  • $this->status_id returns a value of 10

Published

Constant: Page::STATUS_PUBLISHED

Once “published”, a page is viewable on the frontend by any visitor to the website.

  • it will be listed by $this->children()
  • it will be found by $this->find('its_uri')
  • it is possible to access it directly with its full url
  • $this->status_id returns a value of 100

Hidden

Constant: Page::STATUS_HIDDEN

Use this status when you have written a page that you do not want to appear in your site's navigation (e.g., RSS, Sitemap, etc.).

  • it will NOT be listed by $this->children()
  • it will be found by $this->find('its_uri')
  • it is possible to access it directly with its full url
  • $this->status_id returns a value of 101

Note: A “hidden” page can be listed with $this->children(array(), array(), true), because children() can include hidden pages if the 3rd param is set to “true”.

Reviewed

The “Reviewed” page status is deprecated and no longer available in Wolf versions > 0.6.0.

parent($level)

parent() returns an array of values relating to the parent page of the current page.11) Normally, then, it is not used on its own, but to give some information about the parent page.

Using the $level parameter

$level must be initialized properly if it is to be used:

  • If you do NOT supply $level, you simply get the parent object.
  • If $level > $this->level(), you get FALSE.
  • If $level == $this->level(), you get $this returned.
  • Otherwise it tries the above tests with the parent object.

Thus, if a value is given for $level, it should be one of: null, equal-to-current-level, or greater-than-current-level.

Examples

For the following tree of pages:

Home Page
  |
  |- Hickory
  |    |
  |    |- Dickory
  |    |
  |    |- Dock
  |
  |- Clock
  • when Clock is the current page, then:
    • $this->parent()->title() returns “Home Page”
    • $this->parent()->slug() returns ' ' (i.e., null)
    • $this->parent()->level() returns “0”
  • when Dock is the current page, then:
    • $this->parent()->title() returns “Hickory”
    • $this->parent(2)->title() returns “Dock” (this page is at level 2)
    • $this->parent()->slug() returns “hickory”
    • $this->parent()->level() returns “1”
    • etc. …
  • when Home Page is the current page, then:
    • $this->parent()->ANYTHING returns an ERROR (do not use it!)

Wolf provides a simple “previous” and ”next” function. previous returns an array of values relating to the page preceding the current page, if it exists.12) It is thus not used on its own, but normally would be used in the Layout to provide a link to the “preceding” page.

Example

The following code tests to see if a “previous” page exists. If it does, it makes a link accompanied by a label with a double-arrow pointing left:

<?php if ($previous = $this->previous()): ?>
  <div style="float: left; border-top: thin solid #ccc; padding-top: 4px;"><?php echo $previous->link(); ?> &#171; Previous</div>
<?php endif; ?>

See also next.

slug()

The slug is the form of the page's title used in the URL. It is produced by Wolf automatically when the page is created. For example, the “slug” for the “Articles” page is articles. Spaces are converted to hyphens: the title “Rhythm and Blues” will produce the slug rhythm-and-blues.

This value can be changed manually by editing it directly. When editing a page, click on the “Metadata” tab; “Slug” is the first field you see.

The slug of a given page can be retrieved with:

<?php echo $this->slug(); ?>

Notes

  • Home Page has no slug, not ever. For this reason, Wolf CMS does not offer a “slug” field in the metadata area of the root page.
  • Since the “slug” is used to form the page's URL, be careful which characters you use.
  • If the value of the slug is manually edited, it will retain the manually-edited value, even if the title of the page is changed.
  • If you wish to get the slug of a page with the slugs of all its ancestor pages, use the getUri() function.

Examples

find

The find() function uses the slug value to find a page. To find the Articles page, use $this->find('/articles/').

Using the slug in conditions

The slug is the best value to use if you want to ensure that code is used on a certain page, or to prevent some code from executing on a given page. For example, if you want to prevent some code from running on your “blog”-type pages (Articles), you could use something like this:

<?php if ($this->slug() != 'articles') {
  // do stuff you want to happen on all pages but the articles page
  code ... code ... code;
} ?>

tags()

tags() produces an array of the tags which have been entered in the “tags” field under the Metadata tab when editing a page.

Usage

Wolf's default installation gives a simple demonstration of a list of tags in the “Articles” page, where the following line is included:

tags: <?php echo join(', ', $article->tags()); ?>

This shows you how to turn the tags() array into string, and this can then be used for other purposes.

Plugin

The most obvious way of handling tags is by using the Tagger plugin which is under active development. Its growing feature set allows for a range of tag-management and usage options.

Examples

Subpages filtered by tag

If you wanted to give the list of subpages to a parent page, based on the presence of a particular tag, you could do it this way:

<?php $findTag = 'hello'; // REPLACE hello WITH THE TAG YOU WANT TO FIND ?>
<h3>Pages with the tag "<?php echo $findTag; ?>":</h3>
<ul>
<?php foreach ($this->children() as $child): ?>
<?php $childTags = join(',', $child->tags()); ?>
  <?php if (strpos($childTags, $findTag) !== FALSE) : ?>
    <li><?php echo $child->link(); ?></li>
  <?php endif; ?>
<?php endforeach; ?>
</ul>

You manually enter the tag you want to find (“hello” is used in the example above), which is echoed in a heading to your list of pages. Then the foreach loop finds the child pages, while the strpos test filters those pages with the desired tag, which is then output to an unordered list. Include that block of code on the parent page, ensuring that the page's text filter is not TinyMCE, which does not cope with PHP!

Although this code is for use on a “parent” page to find a certain tag-term among its child pages, it could be modified to look in other “branches” of the site, or to look through deeper levels.

$this->

Properly speaking, this is not a “Wolf” function at all. PHP uses a special variable, $this->, which always points to the “current object”. But what is the “current object”? In Wolf, the meaning of $this-> depends on where you use it. Here is it how works:

  • in the Layout, $this-> points at currently displaying page, whatever it is.
  • in the Body of page, $this-> points at that page only.
  • in a Page-part, $this-> points at the page to which that part belongs — even if it is “inherited” by child pages with the “true” condition as in:
    • <?php $this->content('sidebar', true); ?>
    • For example, if the “sidebar” of “Homepage” uses $this->, and that sidebar is inherited by child pages, then $this-> still refers to the homepage.
  • in a Snippet, $this-> will behave as outlined above, depending on whether you call the snippet in your layout, in the body of a page, or in a page-part.

That can be summarized in a table as follows:

If $this-> appears in… …then $this-> points to:
Layout currently displayed page
Body of page that specific page only
Page-part (tab) the owning page only
Snippet as above, depending on where snippet is called

title()

The title() function returns the title of the current page. This code:

<?php echo $this->title(); ?>

will return the text found in the “Page Title” field, prominently displayed when editing a page.

Notes

This function does not take any parameters, but it can be used in conjunction with the find() function to display the title of a page that is not current.

Another example of its use can be found in “header” snippet for the Simple layout to generate Wolf's main navigation:

<li><?php echo $menu->link($menu->title, (in_array($menu->slug, explode('/', $this->url)) ? ' class="current"': null)); ?></li>

updater()

Wolf saves the id of the user who updates a page, and the name for that id can be displayed using updater(). This is sometimes given in the page's “meta”, for example:

<p class="meta">Updated by <?php echo $this->updater(); ?></p>

See also the author() function.

updaterId()

Wolf saves the unique id number of the user who updates a page, and that id number can be accessed using updaterId().

For notes on usage, see the authorId() function page.

url()

The url() function gives access to the URL of the current page. It does not take any parameters. If, for example, the current page was the “Articles” page, then

<?php echo $this->url(); ?>

returns the value:

http://mywolfsite.com/articles

See also: urlById()

Usage notes

URLs of pages that are not current

To return the URL of a page that is not current, use the find function. This code:

<?php echo $this->find('articles')->url(); ?>

returns: http://mywolfsite.com/articles, from anywhere in the site.

Using a Suffix

If you have set up Wolf to use a URL suffix to simulate static pages (e.g., ”.html”), then you might run into problems when embedding url() in a concatenated string. So, for example, in the “sidebar” code of the default “Articles” page, this code:

<?php echo $this->url() .'/'. $date . URL_SUFFIX; ?>

produces an error, because ”.html” is embedded before the $date. In this case, omit the suffix by using the “false” flag:

<?php echo $this->url(false) .'/'. $date . URL_SUFFIX; ?>

This will make “url” omit the unwanted suffix.

Creating links

Although url() can of course be used for creating links, there is a specialized function that is normally the preferred way of doing this: see the documentation on the link() function.

url_match()

This is a simple conditional function which tests the current URI against a parameter supplied. If they match, it returns “true”; otherwise it returns “false”.

Example

A useful example is found in the default “header” snippet that is created when Wolf is installed. In the navigation code, this is the first entry in the <ul> list:

<li><a<?php echo url_match('/') ? ' class="current"': ''; ?> href="<?php echo URL_PUBLIC; ?>">Home</a></li>

This hard-sets the “Home” link in the navigation; the url_match() function provides the test for whether the homepage is current, and echoes the class="current" for styling the anchor if the test is successful.

Notes

All elements of the URI must be matched for “true” to be returned. For example, with the URL http://wolfsite.com/page/child-page, the test url_match('child-page') will return false, but url_match(page/child-page) will return true.

See also the url_start_with() function.

url_start_with()

This is a simple conditional function which tests the current URI against a parameter supplied. If the paratmer matches the first element in the URI, it returns “true”; otherwise it returns “false”.

Examples

Consider the URL http://wolfsite.com/produce/fruit/apples.

  • url_start_with('fruit') will only return “false”.

Usage

This could be a useful function for using specific banner images in different sections of a website. For example, if there is a JPG image which correspsonds to the each top-level page in the site (like “produce”, above), it could be included this way:

//find the top-level page slug, and save it to a variable, e.g. $topSlug, then
<img src="<?php echo (url_start_with($topSlug)) ? $topSlug : 'default'; ?>.jpg" />

This banner would then be used for all the child-pages (e.g. “fruit” and “apples” in the example above) for that top page.

See also the url_match() function.

use_helper()

Helpers” are PHP files which extend the functionality of Wolf CMS on a “per-page” basis. Unlike plugins which provide extra functions throughout a Wolf site, “helpers” only work on the pages in which they are called. They are called this way:

<?php use_helper('Helper_name'); ?>

For information on the helpers supplied with Wolf CMS, and further notes on usage, see the ”Overview of Helpers” page.

Final word

This book is an experimental product and consists of dynamically generated content consisting of other wolfcms.org wiki pages. We hope you like it!

If you enjoy and use Wolf CMS, consider donating to the project.

License applicable to this content

This content is licensed under the Creative Commons Attribution Non-Commercial Share Alike license.

That license lets others remix, tweak, and build upon this work non-commercially, as long as they credit the original author and license their new creations under the identical terms. Others can download and redistribute this work just like the by-nc-nd license, but they can also translate, make remixes, and produce new stories based on this work. All new work based on this work will carry the same license, so any derivatives will also be non-commercial in nature.

For the full license details, please see the original license text at:

http://creativecommons.org/licenses/by-nc-sa/3.0/ (Human readable)

http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode (Full legal text)

1) It can be used, then, as a secondary “page title” field.
2) , 9) , 11) Consult the documentation on $this-> to find out what the “current” page is in different situations.
3) Consult the documentation on creating a page for a full list of page-status definitions.
4) See the children() function documentation for how to include “hidden” pages.
5) There is a yet more full list of constants provided with the Plugin documentation, but without some of the discussion given here.
6) That is, pages at level “2”; see the level documentation for explanations of levels in Wolf.
7) See the content function entry for fuller explanation.
8) Added to core at Revision 172.
10) , 12) Consult the documentation on this to find out what the “current” page is in different situations.
 
books/functions.txt · Last modified: 2011-09-12 00:46 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 wolfcms.org / design by yello studio / Wolf CMS Inside