用户自定义标签

Contents

共享你的标签

请共享你的标签: 点击这里.

简单描述

用户自定义标签是将PHP代码插入到你的站点中的简单方法。为了使用它们,你只需要简单地创建一个标签并将你的PHP代码(除了"<?php" and "?>")放入标签,然后你可以使用smarty象这样调用:{tag_name}
标准安装中已经有一个用户自定义标签存在,你可以使用{user_agent}调用。

标签的参数

你可以传递参数给标签,例如:"{image_link src='hedgehog'}"和标签代码
  echo '<a href="' . $params['src'] . '.jpg" target="_blank">';
  echo '<img src="t_' . $params['src'] . '.jpg"></a>';

详细描述

如何生成一个用户自定义标签(UDT)

Like most things in CMS Made Simple, adding a new plug-in is simple, although it's not quite like a holiday.
To add your own plugin follow these steps:
  1. The plugin editor is in the back-end so you need to login as admin, or a user with appropriate permissions.
  2. In the Admin Console click on "Extensions/User Defined Tags" in the top drop-down menu.
  3. At the bottom of the page click 'Add User Defined Tag'.
  4. In the 'Name' text-box type the name of the tag (it can only contain letters, numbers and underscores). This is what you'll have to type in parenthesies to add a tag into a page so be descriptive but don't make it long.
  5. In the 'Code' text-box type the php code that the tag will be replaced with when the page is requested. (Check the next section for more info)
  6. Click on the 'Submit'.

第一个UDT

Instead of me rambling on about every last detail of plugins and php a simple example should help you get started. Following in the footsteps of all introductions to programming we'll try a hello world script first.
Follow the steps above to create a new plug-in, 'Name' it "helloworld" (no quotes) and in the 'Code' box type/paste this code...
  echo 'Hello World!';
Click 'Submit'. To test the module, create a new 'Content Page' (see Add Content), type "{helloworld}" (no quotes) somewhere in the body and then click 'Preview'. Instead of seeing {helloworld} you should see "Hello World!".
The echo command just writes what's in the quotes so we can also use this to add HTML, or even DHTML or Javascript objects. Try out this code, just edit the plugin we made by clicking on the edit icon next to it on the 'Plugin Managment' page. Try this code...
  echo '<h3>Hello World!</h3>';
Test it in the same way as with the last one, you should now see something that looks a bit like this:

Hello World!

"But you can do all that with a html blob!" I hear you cry. Well, plugins get really useful when you start to add parameters. Parameters alow you to specify something in the tag. Let's say that we wanted to say hello to someone called Bob, try this code...
  echo '<h3>Hello ' . $params['name'] . '!</h3>';
This had added a parameter, "name", to the plugin, the contents of that parameter will be put there when the plugin is called from a tag in a page. Test it in the same way as before, but instead of using "{helloworld}" use "{helloworld name='Bob'}" as the tag to define the name parameter. You should see something like this...

Hello Bob!

That just about covers writing basic plugins, you can do some useful things with the echo command, parameters and a little imagination so just think what you can do if you learn a little PHP...

Passing the page content as a parameter

You can access the page content in a user defined tag by passing it as a parameter:
In your template:
  {content assign=pagecontent}
  {table_of_contents thepagecontent="$pagecontent"}
In your user defined tag named "table_of_contents":
  echo $params['thepagecontent']; // Display page content.
I use this so I can parse to the page content to automatically create a table of contents.

How to get the URL of a page from its content_id

This assumes your page content_id is in a variable named $page_content_id
  global $gCms;
  $hm =& $gCms->GetHierarchyManager();
  $curnode =& $hm->getNodeById($page_content_id);
  $curcontent =& $curnode->GetContent();
  echo 'Page URL: ' . $curcontent->GetURL();

How to Execute Smarty Tags from A User Defined Tag (UDT)

You can do it like this:
  global $gCms;
  $smarty = &$gCms->GetSmarty();
  $smarty_data = "{menu}";
  $smarty->_compile_source('temporary template', $smarty_data, $_compiled );
  @ob_start();
  $smarty->_eval('?>' . $_compiled);
  $_contents = @ob_get_contents();
  @ob_end_clean();
  echo $_contents;
This may allow you to manage multiple domains with one install of CMSMS if you did something like this:
  global $gCms;
  $smarty = &$gCms->GetSmarty();
 
  $url = $_SERVER['REQUEST_URI']; 
  if(eregi('domain1',$url))
  { 
    $smarty_data = "{menu template='cssmenu.tpl'}";
   
  }
  else if(eregi('domain2',$url))
  { 
    $smarty_data = "{menu template='cssmenu2.tpl'}";
  
  }
   $smarty->_compile_source('temporary template',    
     $smarty_data, $_compiled );
     @ob_start();
     $smarty->_eval('?>' . $_compiled);
     $_contents = @ob_get_contents();
     @ob_end_clean();
     echo $_contents;

How to provide variables from a UDT to your page/template

Sometimes you need to provide calculated or processed information via PHP variables from a UDT to a page or template. To do this add a new UDT and call it e.g. "Var" and fill in the code below

global $gCms;
$smarty = &$gCms->GetSmarty();
$foo= "test"; // Variable you want to provide
$smarty->assign('foo', $foo); // make variable "foo" visible for CMSms

Now you are able to use the variable in pages or templates via Smarty:

{Var} {* Call the UDT "Var" before using its variable(s) *}
<p>The content of the variable "foo" is {$foo}.</p>

Output:

The content of the variable "foo" is test.

In the section Share your tags here you will find a more reasonable example (see "Check for expire dates")
This page in: English - Deutsch - Español - Français - Italiano - Lietuvių - Nederlands - Norsk - Polski - Česky - Русский - Svenska - Tiếng Việt - عربي - 日本語 简体中文

User Handbook/Admin Panel/Extensions/User Defined Tags/zh

From CMSMS

Arvixe - A CMSMS Partner