Load Remote File with TWIG












3















Using Twig in Symfony 2.3 I need to be able to select remote assets inside a twig template.



So I have a twig template with a body block like this:



{% block body %}
{% include 'http://asset.remotelocation.co.uk/template.html.twig' %}
{% endblock %}


and as you can see its trying to include a remote twig template. Is this possible as symfony just errors saying it cant find the template?



The twig template location is correct in my code as I can browse to the template url in my browser.



Any help is much appeciated. =)



P.S the remote location is just one of our other webserver where we hold shared assets.










share|improve this question





























    3















    Using Twig in Symfony 2.3 I need to be able to select remote assets inside a twig template.



    So I have a twig template with a body block like this:



    {% block body %}
    {% include 'http://asset.remotelocation.co.uk/template.html.twig' %}
    {% endblock %}


    and as you can see its trying to include a remote twig template. Is this possible as symfony just errors saying it cant find the template?



    The twig template location is correct in my code as I can browse to the template url in my browser.



    Any help is much appeciated. =)



    P.S the remote location is just one of our other webserver where we hold shared assets.










    share|improve this question



























      3












      3








      3


      3






      Using Twig in Symfony 2.3 I need to be able to select remote assets inside a twig template.



      So I have a twig template with a body block like this:



      {% block body %}
      {% include 'http://asset.remotelocation.co.uk/template.html.twig' %}
      {% endblock %}


      and as you can see its trying to include a remote twig template. Is this possible as symfony just errors saying it cant find the template?



      The twig template location is correct in my code as I can browse to the template url in my browser.



      Any help is much appeciated. =)



      P.S the remote location is just one of our other webserver where we hold shared assets.










      share|improve this question
















      Using Twig in Symfony 2.3 I need to be able to select remote assets inside a twig template.



      So I have a twig template with a body block like this:



      {% block body %}
      {% include 'http://asset.remotelocation.co.uk/template.html.twig' %}
      {% endblock %}


      and as you can see its trying to include a remote twig template. Is this possible as symfony just errors saying it cant find the template?



      The twig template location is correct in my code as I can browse to the template url in my browser.



      Any help is much appeciated. =)



      P.S the remote location is just one of our other webserver where we hold shared assets.







      php twig symfony-2.3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 26 '13 at 22:37









      Alain Tiemblo

      26.2k991133




      26.2k991133










      asked Sep 26 '13 at 14:15









      azzy81azzy81

      1,7152134




      1,7152134
























          1 Answer
          1






          active

          oldest

          votes


















          5














          You can create a function that will download this file for you, and make it available for twig.



          The idea :




          • You create a directory app/Resources/views/temp, so a twig files can be accessed at :temp:file.html.twig

          • In your twig file, you'll use a remote_file() function to wrap the first include's argument

          • Your files will be downloaded by your function inside temp directory with a random name

          • your function will return a twig path to access the file locally (the :temp:file.html.twig)

          • don't forget to automate something to clear too old temp files! (a cron?)


          The directory



          Create a temp directory so that your symfony directory tree looks like this:



          enter image description here



          The extension



          Inside your bundle, create a TwigExtension directory. There, create a RemoteFileExtension.php file with the following code. Note: don't forget to replace my namespaces by yours.



          <?php

          namespace FuzTestBundleTwigExtension;

          use SymfonyComponentHttpKernelKernelInterface;

          class RemoteFileExtension extends Twig_Extension
          {

          private $kernel;

          public function __construct(KernelInterface $kernel)
          {
          $this->kernel = $kernel;
          }

          public function getFunctions()
          {
          return array(
          'remote_file' => new Twig_Function_Method($this, 'remote_file'),
          );
          }

          public function remote_file($url)
          {
          $contents = file_get_contents($url);
          $file = $this->kernel->getRootDir() . "/Resources/views/temp/" . sha1($contents) . '.html.twig';
          if (!is_file($file))
          {
          file_put_contents($file, $contents);
          }
          return ':temp:' . basename($file);
          }

          public function getName()
          {
          return 'remote_file';
          }

          }


          Inside your services.yml configuration file, add the following:



          below parameters:



          fuz_tools.twig.remote_file_extension.class: FuzTestBundleTwigExtensionRemoteFileExtension


          below services:



          fuz_tools.twig.remote_file_extension:
          class: '%fuz_tools.twig.remote_file_extension.class%'
          arguments: ['@kernel']
          tags:
          - { name: twig.extension }


          Test it!



          I created an existing http://localhost:8888/test.html.twig. It only contains:



          Hello, {{ name }}! 


          Inside my application, I put the following line :



          {% include remote_file('http://localhost:8888/test.html.twig') with {'name': 'Alain'} %}


          When I run my code, I get :



          enter image description here



          Some more notes



          You should consider that a twig file takes part of your application. A twig file is not an asset, as it needs to be intepreted by Symfony2, with some logic, with some optimizations and so on. What you're doing actually is comparable to a remote include of a PHP file before executing it, strange architecture I think.



          Anyway, your question was interesting, good luck for your implementation.






          share|improve this answer


























          • wow Alain top marks for such a complete answer. Im off to test this out and shall be back with my outcomes soon. Thank you =)

            – azzy81
            Sep 27 '13 at 10:30











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19030721%2fload-remote-file-with-twig%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          You can create a function that will download this file for you, and make it available for twig.



          The idea :




          • You create a directory app/Resources/views/temp, so a twig files can be accessed at :temp:file.html.twig

          • In your twig file, you'll use a remote_file() function to wrap the first include's argument

          • Your files will be downloaded by your function inside temp directory with a random name

          • your function will return a twig path to access the file locally (the :temp:file.html.twig)

          • don't forget to automate something to clear too old temp files! (a cron?)


          The directory



          Create a temp directory so that your symfony directory tree looks like this:



          enter image description here



          The extension



          Inside your bundle, create a TwigExtension directory. There, create a RemoteFileExtension.php file with the following code. Note: don't forget to replace my namespaces by yours.



          <?php

          namespace FuzTestBundleTwigExtension;

          use SymfonyComponentHttpKernelKernelInterface;

          class RemoteFileExtension extends Twig_Extension
          {

          private $kernel;

          public function __construct(KernelInterface $kernel)
          {
          $this->kernel = $kernel;
          }

          public function getFunctions()
          {
          return array(
          'remote_file' => new Twig_Function_Method($this, 'remote_file'),
          );
          }

          public function remote_file($url)
          {
          $contents = file_get_contents($url);
          $file = $this->kernel->getRootDir() . "/Resources/views/temp/" . sha1($contents) . '.html.twig';
          if (!is_file($file))
          {
          file_put_contents($file, $contents);
          }
          return ':temp:' . basename($file);
          }

          public function getName()
          {
          return 'remote_file';
          }

          }


          Inside your services.yml configuration file, add the following:



          below parameters:



          fuz_tools.twig.remote_file_extension.class: FuzTestBundleTwigExtensionRemoteFileExtension


          below services:



          fuz_tools.twig.remote_file_extension:
          class: '%fuz_tools.twig.remote_file_extension.class%'
          arguments: ['@kernel']
          tags:
          - { name: twig.extension }


          Test it!



          I created an existing http://localhost:8888/test.html.twig. It only contains:



          Hello, {{ name }}! 


          Inside my application, I put the following line :



          {% include remote_file('http://localhost:8888/test.html.twig') with {'name': 'Alain'} %}


          When I run my code, I get :



          enter image description here



          Some more notes



          You should consider that a twig file takes part of your application. A twig file is not an asset, as it needs to be intepreted by Symfony2, with some logic, with some optimizations and so on. What you're doing actually is comparable to a remote include of a PHP file before executing it, strange architecture I think.



          Anyway, your question was interesting, good luck for your implementation.






          share|improve this answer


























          • wow Alain top marks for such a complete answer. Im off to test this out and shall be back with my outcomes soon. Thank you =)

            – azzy81
            Sep 27 '13 at 10:30
















          5














          You can create a function that will download this file for you, and make it available for twig.



          The idea :




          • You create a directory app/Resources/views/temp, so a twig files can be accessed at :temp:file.html.twig

          • In your twig file, you'll use a remote_file() function to wrap the first include's argument

          • Your files will be downloaded by your function inside temp directory with a random name

          • your function will return a twig path to access the file locally (the :temp:file.html.twig)

          • don't forget to automate something to clear too old temp files! (a cron?)


          The directory



          Create a temp directory so that your symfony directory tree looks like this:



          enter image description here



          The extension



          Inside your bundle, create a TwigExtension directory. There, create a RemoteFileExtension.php file with the following code. Note: don't forget to replace my namespaces by yours.



          <?php

          namespace FuzTestBundleTwigExtension;

          use SymfonyComponentHttpKernelKernelInterface;

          class RemoteFileExtension extends Twig_Extension
          {

          private $kernel;

          public function __construct(KernelInterface $kernel)
          {
          $this->kernel = $kernel;
          }

          public function getFunctions()
          {
          return array(
          'remote_file' => new Twig_Function_Method($this, 'remote_file'),
          );
          }

          public function remote_file($url)
          {
          $contents = file_get_contents($url);
          $file = $this->kernel->getRootDir() . "/Resources/views/temp/" . sha1($contents) . '.html.twig';
          if (!is_file($file))
          {
          file_put_contents($file, $contents);
          }
          return ':temp:' . basename($file);
          }

          public function getName()
          {
          return 'remote_file';
          }

          }


          Inside your services.yml configuration file, add the following:



          below parameters:



          fuz_tools.twig.remote_file_extension.class: FuzTestBundleTwigExtensionRemoteFileExtension


          below services:



          fuz_tools.twig.remote_file_extension:
          class: '%fuz_tools.twig.remote_file_extension.class%'
          arguments: ['@kernel']
          tags:
          - { name: twig.extension }


          Test it!



          I created an existing http://localhost:8888/test.html.twig. It only contains:



          Hello, {{ name }}! 


          Inside my application, I put the following line :



          {% include remote_file('http://localhost:8888/test.html.twig') with {'name': 'Alain'} %}


          When I run my code, I get :



          enter image description here



          Some more notes



          You should consider that a twig file takes part of your application. A twig file is not an asset, as it needs to be intepreted by Symfony2, with some logic, with some optimizations and so on. What you're doing actually is comparable to a remote include of a PHP file before executing it, strange architecture I think.



          Anyway, your question was interesting, good luck for your implementation.






          share|improve this answer


























          • wow Alain top marks for such a complete answer. Im off to test this out and shall be back with my outcomes soon. Thank you =)

            – azzy81
            Sep 27 '13 at 10:30














          5












          5








          5







          You can create a function that will download this file for you, and make it available for twig.



          The idea :




          • You create a directory app/Resources/views/temp, so a twig files can be accessed at :temp:file.html.twig

          • In your twig file, you'll use a remote_file() function to wrap the first include's argument

          • Your files will be downloaded by your function inside temp directory with a random name

          • your function will return a twig path to access the file locally (the :temp:file.html.twig)

          • don't forget to automate something to clear too old temp files! (a cron?)


          The directory



          Create a temp directory so that your symfony directory tree looks like this:



          enter image description here



          The extension



          Inside your bundle, create a TwigExtension directory. There, create a RemoteFileExtension.php file with the following code. Note: don't forget to replace my namespaces by yours.



          <?php

          namespace FuzTestBundleTwigExtension;

          use SymfonyComponentHttpKernelKernelInterface;

          class RemoteFileExtension extends Twig_Extension
          {

          private $kernel;

          public function __construct(KernelInterface $kernel)
          {
          $this->kernel = $kernel;
          }

          public function getFunctions()
          {
          return array(
          'remote_file' => new Twig_Function_Method($this, 'remote_file'),
          );
          }

          public function remote_file($url)
          {
          $contents = file_get_contents($url);
          $file = $this->kernel->getRootDir() . "/Resources/views/temp/" . sha1($contents) . '.html.twig';
          if (!is_file($file))
          {
          file_put_contents($file, $contents);
          }
          return ':temp:' . basename($file);
          }

          public function getName()
          {
          return 'remote_file';
          }

          }


          Inside your services.yml configuration file, add the following:



          below parameters:



          fuz_tools.twig.remote_file_extension.class: FuzTestBundleTwigExtensionRemoteFileExtension


          below services:



          fuz_tools.twig.remote_file_extension:
          class: '%fuz_tools.twig.remote_file_extension.class%'
          arguments: ['@kernel']
          tags:
          - { name: twig.extension }


          Test it!



          I created an existing http://localhost:8888/test.html.twig. It only contains:



          Hello, {{ name }}! 


          Inside my application, I put the following line :



          {% include remote_file('http://localhost:8888/test.html.twig') with {'name': 'Alain'} %}


          When I run my code, I get :



          enter image description here



          Some more notes



          You should consider that a twig file takes part of your application. A twig file is not an asset, as it needs to be intepreted by Symfony2, with some logic, with some optimizations and so on. What you're doing actually is comparable to a remote include of a PHP file before executing it, strange architecture I think.



          Anyway, your question was interesting, good luck for your implementation.






          share|improve this answer















          You can create a function that will download this file for you, and make it available for twig.



          The idea :




          • You create a directory app/Resources/views/temp, so a twig files can be accessed at :temp:file.html.twig

          • In your twig file, you'll use a remote_file() function to wrap the first include's argument

          • Your files will be downloaded by your function inside temp directory with a random name

          • your function will return a twig path to access the file locally (the :temp:file.html.twig)

          • don't forget to automate something to clear too old temp files! (a cron?)


          The directory



          Create a temp directory so that your symfony directory tree looks like this:



          enter image description here



          The extension



          Inside your bundle, create a TwigExtension directory. There, create a RemoteFileExtension.php file with the following code. Note: don't forget to replace my namespaces by yours.



          <?php

          namespace FuzTestBundleTwigExtension;

          use SymfonyComponentHttpKernelKernelInterface;

          class RemoteFileExtension extends Twig_Extension
          {

          private $kernel;

          public function __construct(KernelInterface $kernel)
          {
          $this->kernel = $kernel;
          }

          public function getFunctions()
          {
          return array(
          'remote_file' => new Twig_Function_Method($this, 'remote_file'),
          );
          }

          public function remote_file($url)
          {
          $contents = file_get_contents($url);
          $file = $this->kernel->getRootDir() . "/Resources/views/temp/" . sha1($contents) . '.html.twig';
          if (!is_file($file))
          {
          file_put_contents($file, $contents);
          }
          return ':temp:' . basename($file);
          }

          public function getName()
          {
          return 'remote_file';
          }

          }


          Inside your services.yml configuration file, add the following:



          below parameters:



          fuz_tools.twig.remote_file_extension.class: FuzTestBundleTwigExtensionRemoteFileExtension


          below services:



          fuz_tools.twig.remote_file_extension:
          class: '%fuz_tools.twig.remote_file_extension.class%'
          arguments: ['@kernel']
          tags:
          - { name: twig.extension }


          Test it!



          I created an existing http://localhost:8888/test.html.twig. It only contains:



          Hello, {{ name }}! 


          Inside my application, I put the following line :



          {% include remote_file('http://localhost:8888/test.html.twig') with {'name': 'Alain'} %}


          When I run my code, I get :



          enter image description here



          Some more notes



          You should consider that a twig file takes part of your application. A twig file is not an asset, as it needs to be intepreted by Symfony2, with some logic, with some optimizations and so on. What you're doing actually is comparable to a remote include of a PHP file before executing it, strange architecture I think.



          Anyway, your question was interesting, good luck for your implementation.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 27 '13 at 13:23

























          answered Sep 26 '13 at 22:36









          Alain TiembloAlain Tiemblo

          26.2k991133




          26.2k991133













          • wow Alain top marks for such a complete answer. Im off to test this out and shall be back with my outcomes soon. Thank you =)

            – azzy81
            Sep 27 '13 at 10:30



















          • wow Alain top marks for such a complete answer. Im off to test this out and shall be back with my outcomes soon. Thank you =)

            – azzy81
            Sep 27 '13 at 10:30

















          wow Alain top marks for such a complete answer. Im off to test this out and shall be back with my outcomes soon. Thank you =)

          – azzy81
          Sep 27 '13 at 10:30





          wow Alain top marks for such a complete answer. Im off to test this out and shall be back with my outcomes soon. Thank you =)

          – azzy81
          Sep 27 '13 at 10:30


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19030721%2fload-remote-file-with-twig%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Coverage of Google Street View

          Full-time equivalent

          Surfing