Jump to content
RichyRich

Hanna Code & _func.php

Recommended Posts

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?

Share this post


Link to post
Share on other sites

Might be a namespace issue - try calling it with:

$summ = \wordLimiter($properties->body);

or:

$summ = \ProcessWire\wordLimiter($properties->body);

Not sure what namespace your _func.php file is in.

Share this post


Link to post
Share on other sites
5 minutes ago, adrian said:

Might be a namespace issue - try calling it with:


$summ = \wordLimiter($properties->body);

or:


$summ = \ProcessWire\wordLimiter($properties->body);

Not sure what namespace your _func.php file is in.

in _func.php is namespace ProcessWire;

Same errors when given fix was attempted.

Share this post


Link to post
Share on other sites

Not sure I have tried to access a function inside a Hanna code before so defining in the code might make sense, or include() 'ing the file.

Or in the Hanna code wrap the function definition in a if(function_exists()) wrapper so the function will only be defined once.

Share this post


Link to post
Share on other sites

Or just move the function to site/ready.php or, if that is still too late, site/init.php.

Share this post


Link to post
Share on other sites

Hmm, in the same conditions (functions used in other templates, defined in _func.php) i currently have the same error (Call to undefined function). The _func.php file is included ( include_once() ) without an error but the functions seem not be be defined in Hanna code. Would maybe @ryan know?

Share this post


Link to post
Share on other sites
9 hours ago, loukote said:

Hmm, in the same conditions (functions used in other templates, defined in _func.php) i currently have the same error (Call to undefined function). The _func.php file is included ( include_once() ) without an error but the functions seem not be be defined in Hanna code. Would maybe @ryan know?

I gave up on the idea. It is messy, but I ended up putting function in template PHP. Works that way. I just rather not have it all spit up into different files. Hard to imagine this being a common bug

Share this post


Link to post
Share on other sites

Assuming _func.php is under templates folder, you need to include _func.php in your hanna code like this:

<?php 

include_once $config->paths->templates . '_func.php';

echo "<div class=\"grid_xs-1\">";
foreach ($pages->find("template=property") as $properties) {
// ...

 

  • Like 1

Share this post


Link to post
Share on other sites

@abdus inclusion works, the issue with the function that seems not being defined in what is the current scope. Surely something due to my lack of understanding.

 

Share this post


Link to post
Share on other sites
On 10/27/2017 at 11:35 PM, abdus said:

Assuming _func.php is under templates folder, you need to include _func.php in your hanna code like this:


<?php 

include_once $config->paths->templates . '_func.php';

echo "<div class=\"grid_xs-1\">";
foreach ($pages->find("template=property") as $properties) {
// ...

 

@abdus That's exactly what i supposed, tried, and what did not work. Try to call from that template code a function defined in _func.php...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      I was really unsure of how to actually title this post, so I do apologize (if someone has a better idea, I will gladly edit it). I am using the profields: pagetable field to allow people to create their own "content" (copy, image, button, etc etc) and rearrange it. I also included a field called "column_size" using the RangeSlider set to (1-12).
      I guess I'll clarify a bit more on this. I am using flexbox where the "row" is <section></section> and the columns are <div class="column"></div> have given the "columns"  flex: 1 1 0; so no matter how many columns you have, the columns will automatically adjust for new content. Where my confusion is coming in: If a user has set up 3 copy items (with 12, 5, 7 respectfully for the column_size), how do I actually output this in my template? I was going to use a switch statement to handle the various items which I thought made it quite easy, but with closing sections and columns I have confused myself as I assume I need an if statement to check if the column size is > 12, or = 12 to determine the actual closing/opening of sections. I apologize if I have not made this very clear. I am a bit unsure how to word this let alone to go about this. 
      Im very appreciative of for any insight into this.
       
       
    • By louisstephens
      So I have a form, once completed, will create new pages. All in all, this is eazy-peezy for me now. I guess I need a bit of guidance on how to actually structure the rest of my code.  I thought I could just write a function (_func.php) and pass the fields to the function and let it do its' thing. However, I am kinda hitting a road block when I do it this way. 
      I currently am passing first name, last name, city, state (options field), and making pages based on the first/last names. I guess where I run into some issues is I am trying to check to see if the "page" already exists, and if it does, throw out an error:
      In the home template:
      if(isset( $_POST['submit'])) { $firstName =Trim (stripslashes($_POST['firstname'])); $lastName = Trim(stripslashes($_POST['lastname'])); $fullName = $firstName . $lastName; $city = Trim(stripslashes($_POST['city'])); $state = Trim(stripslashes($_POST['state'])); $lowerCaseName = strtolower($fullName); $people = $pages->find("template=person"); foreach ($people as $person) { $checkFirstName = $person->first_name; $checkLastName = $person->last_name; $checkFullName = $checkFirstName . $checkLastName; if ($checkFullName === $lowerCaseName) { echo "<p>" . "This person has already created a page. Please choose a different name." . "</p>"; } else { echo "hey"; processNewPerson(need_to_pass_person_details_to_function); } } // end foreach In _func.php:
      function processNewPerson($list) { $u = new Page(); $u->template = "person"; $u->parent = wire('pages')->get("/people/"); $u->title = ; $u->first_name = ; $u->last_name = ; $u->state = ; $u->city = ; $u->save(); $u->setOutputFormatting(false); } I am a little unsure of how to actually pass all the information to the template, as well as if this is even the best approach to do this. Would it make more sense to do this in a class, or keep it the way it is?
    • By louisstephens
      I have a repeater with a text field where I would like to use hanna code (to make it easier on the user) so the user can define the action of a image that is in the repeater. For example:
      1. The image should have an external link:
      - the user can just type in the url
      2. The image, when clicked, should be printable (using print.js)
      - the user can just type [[print_image]]
      That works just fine, but I am running into an "issue". The code I need to use to print is:
      onclick="printJS({printable: 'path_to_image', type: 'image'});" I can put that into the hanna code as plain text, but I have no idea how to get the image from the repeater item and pass to "path_to_image". Is this even possible?
    • By hansv
      I got this morning an error on all pages that uses the hanna-code module on site that is already a few years online.  
      I use the latest pw dev
        Als guest you see this error on www.orgelpijpjes.be
      Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Error has been logged. As administrator 
      Error: Exception: SQLSTATE[HY000]: General error: 1036 Table 'hanna_code' is read only (in /customers/9/7/b/orgelpijpjes.be/httpd.www/site/modules/TextformatterHannaCode/TextformatterHannaCode.module line 305) #0 /customers/9/7/b/orgelpijpjes.be/httpd.www/site/modules/TextformatterHannaCode/TextformatterHannaCode.module(305): PDOStatement->execute() #1 /customers/9/7/b/orgelpijpjes.be/httpd.www/site/modules/TextformatterHannaCode/TextformatterHannaCode.module(198): TextformatterHannaCode->getReplacement('inthepicture', Array, true) #2 /customers/9/7/b/orgelpijpjes.be/httpd.www/wire/modules/Fieldtype/FieldtypeText.module(94): TextformatterHannaCode->formatValue(Object(ProcessWire\Page), Object(ProcessWire\Field), '<p>[[inthepictu...') #3 /customers/9/7/b/orgelpijpjes.be/httpd.www/wire/modules/Fieldtype/FieldtypeTextarea.module(119): ProcessWire\FieldtypeText->___formatValue(Object(ProcessWire\Page), Object(ProcessWire\Field) This error message was shown because: you are logged in as a Superuser. Error has been logged Has anyone suggestions how to solve this?
      thx
       
    • By ryanC
      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!
×
×
  • Create New...