ryanC

PHP include in field content

Recommended Posts

Hi, I have php includes in my template file that are working fine....however I need to have some additional includes within a text area field, where I enter my HTML content.

For example, this code at the top of my template file:

<?php $myPath = $config->urls->templates;?>

Allows me to have something like this on every page:

<?php include("includes/footer.inc.php"); ?>

So far so good, but I have a text area field named page_content, where I would like to manually enter additional includes. 

If I enter this in that text area field, I get a photo:

<img src="images/photo.jpg" />

But if I enter this in that same field, nothing happens at all:

<?php include("includes/phone.inc.php"); ?>

Any thoughts? I have reviewed this thread https://processwire.com/api/ref/files/include/ but I don't think it gets at what I'm looking for.

Thanks!

Share this post


Link to post
Share on other sites

CKEditor fields are regular HTML strings, they're not evaluated as PHP. Even then, whatever you write in the field is encoded, so < and > for example will turn into &lt; and &gt; HTML escaped variants.

Your problem can be solved easily with Hanna Code

 

  • Like 5

Share this post


Link to post
Share on other sites

Thanks abdus, does this apply even if the field isn't a regular textarea? I should have added that I am using the AceExtended module. I have a lot of HTML content in there already, which is working fine, like :

<section>
<div class="xyz">
<img src="images/photo.jpg" />
</div>
</section>

I wonder if using a javascript text would be a work around. I did look into that Hannah page, I'd like to use it but it's a little beyond my skill level.

Share this post


Link to post
Share on other sites

It works as long as Hanna Code is added to the field's textformatters.

  • Like 1

Share this post


Link to post
Share on other sites

Here's a sample Hanna code that you can use in text fields as [[include file=test]] where `test` is a php file at templates/includes/test.php.

<?php namespace ProcessWire; 
// test.php

echo 'hello';

?>
<p>regular html</p>

Hanna Code definition is just these lines:

<?php

$file = "{$config->paths->templates}includes/{$file}.php";
if (file_exists($file)) include $file;

Or use this to quickly import into your setup

!HannaCode:include:eyJuYW1lIjoiaW5jbHVkZSIsInR5cGUiOiIyIiwiY29kZSI6Ijw/cGhwXG5cbiRmaWxlID0gXCJ7JGNvbmZpZy0+cGF0aHMtPnRlbXBsYXRlc31pbmNsdWRlc1wveyRpbmNsdWRlfS5waHBcIjtcbmlmICghZmlsZV9leGlzdHMoJGZpbGUpKSByZXR1cm47XG5cbmluY2x1ZGUgJGZpbGU7In0=/!HannaCode

 

  • Like 2

Share this post


Link to post
Share on other sites

Hi, I am trying this Hannah example out, I think I am missing a step.

I created a new php Hannah code called test that contains this:

<?php

$file = "{$config->paths->templates}includes/{$file}.php";
if (file_exists($file)) include $file;

made a new include file at templates/includes with:

<?php namespace ProcessWire; 
// test.php

echo 'hello';

?>
<p>regular html</p>

and in my page content field I have 

[[include file=test]] 

but the page output just gives me "[[include file=test]]"

Share this post


Link to post
Share on other sites

Add hanna code to field's textformatters.

Setup > Fields > <fieldname> > Details > Textformatters

  • Like 1

Share this post


Link to post
Share on other sites

I think I have set that way, I'm attaching a screenshot of all the parts involved (other than the include folder itself).

 

 

hannah-question.jpg

Share this post


Link to post
Share on other sites

You named the hanna code as `test`, it should have been `include` instead. (or use [[test file=test]])

image.png.2af6eb7ec4cedac0a72353572e0cf87d.png

  • Like 3

Share this post


Link to post
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.

  • Similar Content

    • By RichyRich
      Okay folks, I am trying to put a character limit on a body of text. I am using the following function, which is place in the _func.php file:
      function wordLimiter($str, $limit = 120, $endstr = '…'){ $str = strip_tags($str); if(strlen($str) <= $limit) return $str; $out = substr($str, 0, $limit); $pos = strrpos($out, " "); if ($pos>0) { $out = substr($out, 0, $pos); } return $out .= $endstr; }  
      This works just fine on it's own. When placed in Hanna code I get a fatal error, "Call to undefined function wordLimiter()". Code in hanna
      <?php echo "<div class=\"grid_xs-1\">"; foreach ($pages->find("template=property") as $properties) { //attempt at defining $summ to call wordLimitor() $summ = wordLimiter($properties->body); echo " <div class=\"col-5 card property\"> <a href=\"{$properties->url}\"> <img src=\"{$properties->images->first->size(500,500)->url}\"> </a> </div> <div class=\"col-7\"> <h3>{$properties->title}</h3> <p>{$summ}</p> </div> <div class=\"col-12 grid details\"> <div class=\"col\"> <i class=\"fa fa-dollar\" aria-hidden=\"true\"></i> {$properties->price}</div> <div class=\"col\"> <i class=\"fa fa-bed\" aria-hidden=\"true\"></i> {$properties->bedroom}</div> <div class=\"col\"> <i class=\"fa fa-bath\" aria-hidden=\"true\"></i> {$properties->bathroom} </div> <div class=\"col\"> <i class=\"fa fa-arrows\" aria-hidden=\"true\"></i> {$properties->square_feet} SqFt</div> </div>"; } echo "</div>";
      I have also tried placing the function wordLimiter() at the top of the Hanna but it give a different error, " Cannot redeclare wordLimiter() "

      Am I missing something here? Limitation somewhere?
    • By clsource
      Hello,
      I just want to point out this framework for making html apps https://onsen.io/
      it's compatible with jquery, angular, vue, react or any other js framework.
      So far I made an app using this and bootstrap with https://propeller.in/
      for the desing and some libs like jquery and lodash for the app logic
      I prefer this to other alternatives like ionic since it does not bound you to angular or react.
       
      Cheers
    • By louisstephens
      So maybe has already figured this out, but I am stumped. I have a field (test_field) that is set to pdfs, and I was trying to get a count of the number of pdfs in that field so I could add it to a status bubble on the front end. I tried:
      function testPDF() { $a = $page->test_field->count(); echo "<span class=\"bubble\">" . $a . "</span>"; } but it is returning "NULL" . I currently have 3 added to the field. I also tried putting this function in _func.php, though I need to use wire('pages'), but I only need to get the count for that specific page, so I am sort of at a loss of how to proceed.
    • By sirhc
      Hi all,
      Im trying to install this core module and got the error of not having the ImageMagick library installed.
      But if im checking for the version of my ImageMagick installed on my server via SSH im getting this response;
      identify -version
      Version: ImageMagick 6.9.4-9 Q16 i386 2016-06-21 http://www.imagemagick.org
      Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
      License: http://www.imagemagick.org/script/license.php
      Features: Cipher DPC 
      Delegates (built-in): bzlib freetype gslib jng jpeg png ps tiff xml zlib
      Does this even mean the library is installed? And if yes, why cant i install my module?
      Pleaassee help me
      Greetz.
    • By rocket
      Noob to Processwire. Trying to convert some older sites to Pwire. I am having trouble including a legacy form class into my templates:
      If I embed the php directly in the template, it works. But if I use include statements, the form object returns null.
      This works:
          use formbuild\Form;
          session_start();
          $form = new Form(init1, 'init2', ...');
          $form->param1 = ...;
          $form->param2 = ...;
          $form->param3 = ...;
       
          $form->render();
      -----------------------------------------------------------------------------------------
      This fails:
          include_once ($config->paths->templates."forms/form1.php");
          $form->render();
      ____________________________________________________________________
      My referencing is OK, no error message. The only code that cannot reside in the include file are the following lines:
          use formbuild\Form;
          $form = new Form(init1, 'init2', ...');
      My PhP is rusty, maybe that is the problem, but it could be something about Pwire, namespace maybe. I have tested it outside Pwire and no problems.
      Please advise. Thanks.