Exemplo n.º 1
0
/**
 * Smarty plugin
 *
 * @package    Smarty
 * @subpackage PluginsFunction
 */
function smarty_function_field($params, &$smarty)
{
    if (!isset($params['name']) or !($tagName = $params['name'])) {
        return;
    }
    if (!empty($params['assign'])) {
        $assign = (string) $params['assign'];
    }
    $output = '';
    $tagPropString = isset($params['tagPropString']) ? $params['tagPropString'] : "";
    $modx =& $smarty->modx;
    $modx->getParser();
    $nextToken = substr($tagName, 0, 1);
    $cacheable = false;
    if ($nextToken === '#') {
        $tagName = substr($tagName, 1);
    }
    if (is_array($modx->resource->_fieldMeta) && in_array($modx->parser->realname($tagName), array_keys($modx->resource->_fieldMeta))) {
        $element = new modFieldTag($modx);
        $element->set('name', $tagName);
        $element->setCacheable($cacheable);
        $output = $element->process($tagPropString);
    } elseif ($element = $modx->parser->getElement('modTemplateVar', $tagName)) {
        $element->set('name', $tagName);
        $element->setCacheable($cacheable);
        $output = $element->process($tagPropString);
    }
    if (!empty($assign)) {
        $smarty->assign($assign, $output);
        return;
    }
    // else
    return $output;
}
Exemplo n.º 2
0
 /**
  * Processes a modElement tag and returns the result.
  *
  * @param string $tag A full tag string parsed from content.
  * @return mixed The output of the processed element represented by the
  * specified tag.
  */
 public function processTag($tag, $processUncacheable = true)
 {
     $element = null;
     $elementOutput = null;
     $outerTag = $tag[0];
     $innerTag = $tag[1];
     /* collect any nested element tags in the innerTag and process them */
     $this->processElementTags($outerTag, $innerTag, true);
     $outerTag = '[[' . $innerTag . ']]';
     $tagParts = xPDO::escSplit('?', $innerTag, '`', 2);
     $tagName = trim($tagParts[0]);
     $tagPropString = null;
     if (isset($tagParts[1])) {
         $tagPropString = trim($tagParts[1]);
     }
     $token = substr($tagName, 0, 1);
     $tokenOffset = 0;
     $cacheable = true;
     if ($token === '!') {
         if (!$processUncacheable) {
             return $outerTag;
         }
         $cacheable = false;
         $tokenOffset++;
         $token = substr($tagName, $tokenOffset, 1);
     }
     if ($cacheable && $token !== '+') {
         $elementOutput = $this->loadFromCache($outerTag);
     }
     if ($elementOutput === null) {
         switch ($token) {
             case '+':
                 $tagName = substr($tagName, 1 + $tokenOffset);
                 $element = new modPlaceholderTag($this->modx);
                 $element->set('name', $tagName);
                 $element->setTag($outerTag);
                 $elementOutput = $element->process($tagPropString);
                 break;
             case '%':
                 $tagName = substr($tagName, 1 + $tokenOffset);
                 $element = new modLexiconTag($this->modx);
                 $element->set('name', $tagName);
                 $element->setTag($outerTag);
                 $element->setCacheable($cacheable);
                 $elementOutput = $element->process($tagPropString);
                 break;
             case '~':
                 $tagName = substr($tagName, 1 + $tokenOffset);
                 $element = new modLinkTag($this->modx);
                 $element->set('name', $tagName);
                 $element->setTag($outerTag);
                 $element->setCacheable($cacheable);
                 $elementOutput = $element->process($tagPropString);
                 break;
             case '$':
                 $tagName = substr($tagName, 1 + $tokenOffset);
                 if ($element = $this->getElement('modChunk', $tagName)) {
                     $element->set('name', $tagName);
                     $element->setTag($outerTag);
                     $element->setCacheable($cacheable);
                     $elementOutput = $element->process($tagPropString);
                 }
                 break;
             case '*':
                 $tagName = substr($tagName, 1 + $tokenOffset);
                 $nextToken = substr($tagName, 0, 1);
                 if ($nextToken === '#') {
                     $tagName = substr($tagName, 1);
                 }
                 if (is_array($this->modx->resource->_fieldMeta) && in_array($this->realname($tagName), array_keys($this->modx->resource->_fieldMeta))) {
                     $element = new modFieldTag($this->modx);
                     $element->set('name', $tagName);
                     $element->setTag($outerTag);
                     $element->setCacheable($cacheable);
                     $elementOutput = $element->process($tagPropString);
                 } elseif ($element = $this->getElement('modTemplateVar', $tagName)) {
                     $element->set('name', $tagName);
                     $element->setTag($outerTag);
                     $element->setCacheable($cacheable);
                     $elementOutput = $element->process($tagPropString);
                 }
                 break;
             default:
                 $tagName = substr($tagName, $tokenOffset);
                 if ($element = $this->getElement('modSnippet', $tagName)) {
                     $element->set('name', $tagName);
                     $element->setTag($outerTag);
                     $element->setCacheable($cacheable);
                     $elementOutput = $element->process($tagPropString);
                 }
         }
     }
     if ($this->modx->getDebug() === true) {
         $this->modx->log(xPDO::LOG_LEVEL_DEBUG, "Processing {$outerTag} as {$innerTag} using tagname {$tagName}:\n" . print_r($elementOutput, 1) . "\n\n");
         /* $this->modx->cacheManager->writeFile(MODX_BASE_PATH . 'parser.log', "Processing {$outerTag} as {$innerTag}:\n" . print_r($elementOutput, 1) . "\n\n", 'a'); */
     }
     return $elementOutput;
 }