lele_ballack Posted September 29, 2020 Share Posted September 29, 2020 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> <div class="counter-text"> <h3><span class="counter"><?=$counter->Counting?></span><span class="plus-icon">+ </span></h3> <p><?=$counter->Title?></p> </div> </div> </div> <?php } ?> How could I achieve it? How can I pass a variable through a field? Link to comment Share on other sites More sharing options...
diogo Posted September 29, 2020 Share Posted September 29, 2020 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> 2 Link to comment Share on other sites More sharing options...
psy Posted September 29, 2020 Share Posted September 29, 2020 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. <?php /** * 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 = [ 'FieldtypeText', 'FieldtypeTextarea', 'FieldtypeInteger', 'FieldtypeFloat', 'FieldtypeDatetime', 'FieldtypeToggle', 'FieldtypeCheckbox', 'FieldtypePage' ]; $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); break; case 'FieldtypePage': $fldData = $dataPage->$replacement->title; break; case 'FieldtypeCheckbox': $fldData = $dataPage->$replacement == true ? "Yes" : "No"; break; default: $fldData = $dataPage->$replacement; break; } $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; } 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now