I'm writing a magento extension and I need to have a phtml template file that is fetched from an external source. It can change remotely.

Currently I'm using http requests to get the file, and then php file access (file_put_contents()) to write it.

However this method sound kinda risky and 'wrong'. I feel like caching might do some problems and sites that have cluster of servers might not sync those files between them (and maybe other weird behaviors).

So my question is this:

  1. Is there a "magento" why to programatically change phtml files?
  2. If not, do you see any serious dangers / faults with my current implementation?

I know it sound weird to change phtml files but that's what I need to do. Using the block functions just won't cut it (I need a totally different html file that dynamically changes without any firm template I can relay on. I guess I could do the entire phtml is just a call to a block function that returns everything, but that sound silly).



Changing template files on the fly is wrong.
What will you do in case you need to version the files?
My recommendation is to get what you need from the external source and save it somewhere in your db. Then use that to print what you need.

The simplest way to do so is to store data inside core_config_data table.

Let's say content/from/external is a unique key you don't use for anything else.
You can do this to save the external content to your db:

$path = 'content/from/external';
$externalContent = fetch template here from external content
$config = Mage::getModel('core/config_data')->load($path, 'path');

Then you can have one single template that will print this:

<?php echo Mage::getStoreConfig('content/from/external');

you can do some additional processing of the value before printing it if needed.

  • Great input, thanks! I considered using Magento config for that purpose, but two things bother me here: 1. cache. I will need to clear cache whenever getting new template, and I'm not sure about the negative side effects of reseting cache lets say, every 10 minutes for big sites. 2. some templates may be in the size of 10K and more, and I was wondering if its smart to save large config values. what do you think?
    – Ronen Ness
    Jan 6 '16 at 12:04
  • I don't see a problem with saving large config values. But indeed if you clear the cache too often you might get into troubles. Maybe you can save your content directly in the cache (not in the config), with a special tag, and clear that tag only. But I don't have any code for that. Is just an idea.
    – Marius
    Jan 6 '16 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.