1

I am trying to reference a twig file (mapCities.html.twig), from my main bandle, in a custom directive. Can anyone help me with this?

app.directive('customDirective', function() {
  return {
    restrict: 'E',
    templateUrl: 'mapCities.html',
    // template: '<div id="sampleCode">This is sample code</div>',
    replace: true
  }
})
  1. At the moment I am getting an 404 (as expected).
  2. My project is in Symfony2 & AngularJS
4
  • Angular is a front-end library and thus needs front-end access to the file you're trying to reference. Your mapCities.html.twig file is a view file internal to your Symfony app. It must be exposed using some sort of Route -> Controller -> View combo. Commented Mar 30, 2015 at 14:58
  • You are right on this. I changed it with an HTML file but still it doesn't work. I just read that the templateURL can optionally contain a DOM element id when the template is defined in <script> tags. Commented Mar 30, 2015 at 15:02
  • I understand what templateUrl is. You simply can't access a Twig file directly using it. Commented Mar 30, 2015 at 15:05
  • 1
    We agree on this. I just altered my code so instead of a twig file I am trying to reference an html file. but it still doesn't work. Commented Mar 30, 2015 at 15:06

1 Answer 1

4

Twig views are not directly accessible from the front-end. You must set up a route in routing.yml to access the file using the basic rendering controller:

acme_bundle_map_cities:
    path: /mapCities
    defaults:
        _controller: FrameworkBundle:Template:template
        template:    'AcmeBundle:Static:mapCities.html.twig'

Then modify your directive to match this route:

app.directive('customDirective', function() {
  return {
    restrict: 'E',
    templateUrl: '/mapCities',
    // template: '<div id="sampleCode">This is sample code</div>',
    replace: true
  }
});

Make sure you modify your values accordingly.

Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic. I wasn't aware of the basic rendering controller. That'll save some boilerplate in future. Thanks.
You saved my day ! It worked super astonishingly and amazingly great :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.