Jump to content

Recommended Posts

I would like to get some input on the below problem and my proposed solution. Is there a more efficient or elegant way to accomplish the requirements? I'm assuming others may have solved this previously and I see no reason to repeat other's mistakes if I can avoid them.   :-*
 
Problem Statement: I am storing blocks of text that includes references to different 'entities' that will exists in the application. For my example below the entities are 'products' and 'companies'. I will have dual purposes for this text. In some cases, I will want to display the text as plain text (no html) and in others I will want those entities to be a hyperlink to view the record (or some other function). Also, I want the ability to change where those links go without having to modify the stored data. These 'entities' will be the only content within the text to need to use HTML.
 
The Plan: My plan is to store the data with placeholders for the entities that include the entity type and the entity id. I can then use that data to either strip out the placeholder content (for plain text output) or dynamically create the hyperlinks using the entity type and id parameters.
 
How I would store the text

The <entity type='product' id='123'>Widget</entity> is a new product that is available from both <entity type='company' id='456'>ACME Industries</entity> and <entity type='company' id='789'>Dunder Mifflin</entity>.

 
Example code
<?php
//Function to return plain text
function outputPlainText($input)
{
    //Strip out entity tags
    return strip_tags($input);    
}
//Function to return text with hyperlinks
function outputHtmlText($input)
{
    //Find 'entity' tags and parse out parameters
    $pattern = "#<entity type='([^']*)' id='([^']*)'>([^<]*)</entity>#";
    return preg_replace_callback($pattern, 'formatEntityLink', $input);
}
//Function to format hyperlinks
function formatEntityLink($e)
{
    //Format entity match as a valid hyperlink
    //** Actual logic would be more complicated
    //** Will probably use a switch statement for each entity type
    return "<a href='display_record.php?type={$e[1]}&id={$e[2]}'>{$e[3]}</a>";
}
?>
 
Usage #1
echo outputPlainText($content);
Output:

The Widget is a new product that is available from both ACME Industries and Dunder Mifflin.

 
Usage #2
echo outputHtmlText($content);
Output:

The Widget is a new product that is available from both ACME Industries and Dunder Mifflin.

 
Edited by Psycho
Link to comment
https://forums.phpfreaks.com/topic/303422-suggestions-on-replacement-logic/
Share on other sites

Instead of implementing your own ad-hoc template system, how about an actual template engine like Twig? You can then create a macro which has different output depending on a template variable for the mode:

{% macro product(id, text, plaintext) %}{% if plaintext %}{{ text }}{% else %}<a href="#">{{ text }}</a>{% endif %}{% endmacro %}

Usage:

The {{ entities.product(123, "Widget", plaintext) }} is a new product ...
  • Like 1

Thanks for the response Jacques1,

 

I think I am missing something in your response. Are you proposing I store the content as this:

 

The {{ entities.product(123, "Widget", plaintext) }} is a new product ...

 

These blocks of text will be saved in the database for many records. Think of it as a review for each product.

 

The third parameter would have to be dynamic based on the purpose of the output. So, I assume I would define "plaintext" as a template variable (true/false) before executing the text with the twig code. Or maybe I am making some wrong assumptions in your response.

The third parameter would have to be dynamic based on the purpose of the output. So, I assume I would define "plaintext" as a template variable (true/false) before executing the text with the twig code. Or maybe I am making some wrong assumptions in your response.

That's the idea. You'd just do something like

$reviewHtml = $template->render($review, [
  'plaintext' => false
]);
In the Symfony based sites I create I use twig for just about all the template needs I have, whether it be full pages, email messages or just small snippets. I've not yet needed to do anything like your html vs non-html split but if I ever needed to I'd probably go for either a macro or custom function solution, which ever seemed most appropriate.

Thanks for the clarification Kicken. I guess my only objection would be due to my slight OCD with regard to storing "code" for what should logically be "data". I really didn't like the idea of storing custom tags either, but they at seemed more akin to data. I'll just need to create a process to parse the content into the format

 

The {{ entities.product(123, "Widget", plaintext) }} is a new product ...
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.