Pods::template PHP Method

template() public method

Display the page template
See also: Pods_Templates::template
Since: 2.0
public template ( $template_name, string $code = null, boolean $deprecated = false ) : mixed
$code string Custom template code to use instead
$deprecated boolean Whether to use deprecated functionality based on old function usage
return mixed Template output
    public function template($template_name, $code = null, $deprecated = false)
    {
        $out = null;
        $obj =& $this;
        if (!empty($code)) {
            $code = str_replace('$this->', '$obj->', $code);
            // backwards compatibility
            $code = apply_filters('pods_templates_pre_template', $code, $template_name, $this);
            $code = apply_filters("pods_templates_pre_template_{$template_name}", $code, $template_name, $this);
            ob_start();
            if (!empty($code)) {
                // Only detail templates need $this->id
                if (empty($this->id)) {
                    while ($this->fetch()) {
                        echo $this->do_magic_tags($code);
                    }
                } else {
                    echo $this->do_magic_tags($code);
                }
            }
            $out = ob_get_clean();
            $out = apply_filters('pods_templates_post_template', $out, $code, $template_name, $this);
            $out = apply_filters("pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this);
        } elseif (class_exists('Pods_Templates')) {
            $out = Pods_Templates::template($template_name, $code, $this, $deprecated);
        } elseif ($template_name == trim(preg_replace('/[^a-zA-Z0-9_\\-\\/]/', '', $template_name), ' /-')) {
            ob_start();
            $default_templates = array('pods/' . $template_name, 'pods-' . $template_name, $template_name);
            $default_templates = apply_filters('pods_template_default_templates', $default_templates);
            // Only detail templates need $this->id
            if (empty($this->id)) {
                while ($this->fetch()) {
                    pods_template_part($default_templates, compact(array_keys(get_defined_vars())));
                }
            } else {
                pods_template_part($default_templates, compact(array_keys(get_defined_vars())));
            }
            $out = ob_get_clean();
            $out = apply_filters('pods_templates_post_template', $out, $code, $template_name, $this);
            $out = apply_filters("pods_templates_post_template_{$template_name}", $out, $code, $template_name, $this);
        }
        return $out;
    }

Usage Example

 /**
  * Attach Pods Template to $content
  *
  * @param string        $template_name  The name of a Pods Template to load.
  * @param string        $content        Post content
  * @param Pods          $pods           Current Pods object.
  * @param bool|string   $append         Optional. Whether to append, prepend or replace content. Defaults to true, which appends, if false, content is replaced, if 'prepend' content is prepended.
  *
  * @return string $content with Pods Template appended if template exists
  *
  * @since 2.4.5
  */
 function load_template($template_name, $content, $pods, $append = true)
 {
     //prevent infinite loops caused by this method acting on post_content
     remove_filter('the_content', array($this, 'front'));
     $template = $pods->template($template_name);
     add_filter('the_content', array($this, 'front'));
     //check if we have a valid template
     if (!is_null($template)) {
         //if so append it to content or replace content.
         if ($append === 'replace') {
             $content = $template;
         } elseif ($append === 'prepend') {
             $content = $template . $content;
         } elseif ($append || $append === 'append') {
             $content = $content . $template;
         } else {
             $content = $template;
         }
     }
     return $content;
 }