Psycho Posted March 10, 2017 Share Posted March 10, 2017 (edited) 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 March 10, 2017 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/303422-suggestions-on-replacement-logic/ Share on other sites More sharing options...
Jacques1 Posted March 10, 2017 Share Posted March 10, 2017 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 ... 1 Quote Link to comment https://forums.phpfreaks.com/topic/303422-suggestions-on-replacement-logic/#findComment-1544065 Share on other sites More sharing options...
Psycho Posted March 10, 2017 Author Share Posted March 10, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303422-suggestions-on-replacement-logic/#findComment-1544091 Share on other sites More sharing options...
kicken Posted March 11, 2017 Share Posted March 11, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303422-suggestions-on-replacement-logic/#findComment-1544094 Share on other sites More sharing options...
Psycho Posted March 13, 2017 Author Share Posted March 13, 2017 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/303422-suggestions-on-replacement-logic/#findComment-1544190 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.