Jump to content

Render PHP variable inside field


Recommended Posts

I have a counter in my homepage that keeps track of different indicators. Some of them are manually inserted in the back end, while some other should be the results of different page count.

For example, I have this variable that returns the number of articles: 

$articles = $pages->find("template=outcome")->count();

And I would like to insert it in the field 'Counting' that's rendered inside the counter in the homepage:

<?php foreach ($page->Counter as $counter) { ?>
  <div class="col-xl-3 col-lg-3 col-md-6">
    <div class="counter-wrapper single-counter mb-30">
      <div class="counter-icon">
        <i class="<?=$counter->Icon?>"></i>
      <div class="counter-text">
        <h3><span class="counter"><?=$counter->Counting?></span><span class="plus-icon">+ </span></h3>
<?php } ?>

How could I achieve it? How can I pass a variable through a field?

Link to comment
Share on other sites

Is Counting a text field? I imagine you want to do something like: "some text $articles some more text". If so, you can use the Hanna Code module, or even a simple string replace, by preparing the text in the field with a placeholder tag: "some text {articles count} some more text", and in your code: 

<h3><span class="counter"><?=str_replace("{articles count}", $articles, $counter->Counting)?></span><span class="plus-icon">+ </span></h3>


  • Like 2
Link to comment
Share on other sites

Had an extreme example to deal with recently. Multiple field replacements for various fieldtypes. May not be the best approach but worked for me. Private function is in a custom module function.


     * Takes a page field, eg 'body' as a template and replaces tags eg {age} with the same field value
     * from the supplied data page
     * @param $tplPage
     * @param $tplField
     * @param $dataPage
     * @param string $startTag
     * @param string $endTag
     * @param array $other
     * @return string|string[]
     * @throws WireException
     * @throws WirePermissionException
    private function _compileFieldTags ($tplPage, $tplField, $dataPage, $startTag = '{', $endTag = '}', $formatDate = false, $other = []) {

        $allowedFieldtypes = [
        $replacementNames = [];
        $replacementValues = [];

        // Sort out what to do with each inputfield type
        foreach ($dataPage->fieldgroup as $replacement) {
            if (!in_array($replacement->type, $allowedFieldtypes)) continue;

            switch ($replacement->type) {
                case 'FieldtypeDatetime':
                    $fldData = $formatDate == false ? $dataPage->$replacement : $dataPage->getFormatted($replacement);
                case 'FieldtypePage':
                    $fldData = $dataPage->$replacement->title;
                case 'FieldtypeCheckbox':
                    $fldData = $dataPage->$replacement == true ? "Yes" : "No";
                    $fldData = $dataPage->$replacement;
            $replacementNames[] = $replacement->name;
            $replacementValues[] = $fldData;
        // Prepare replacement arrays
        foreach ($dataPage as $k => $v) {
            $replacementNames[] = $startTag . $k . $endTag;
            $replacementValues[] = $v;

        $replacementNames['url'] = $startTag . 'url' . $endTag;
        $replacementValues[] = $dataPage->httpUrl;

        $result = str_ireplace($replacementNames, $replacementValues, $tplPage->$tplField);
        return $result;


  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...