Alternative twig path for Silex controller provider - path

I am creating a new controller provider that I will use across several projects, and I need to have a couple of twig templates inside the directory of this controller provider for use in some of the routes of that provider. I want to avoid having to copy all the template files into the project directory for every project that is going to use this controller provider (to have it under the twig.path specified directory), so my question is this:
How can I render a template that is not located under the twig.path directory? Can I tell the twig service provider to render a specific path, like __DIR__.'/views/some.template.twig' in the controller provider file?

You can access twig loader by using $app['twig.loader.filesystem'].
For example, put this in your controller class in connect method
$app['twig.loader.filesystem']->addPath(
$pathToTemplates
);
There are also exists Twig_Loader_Filesystem::prependPath()
You can also specify namespace for paths. To do this just add name of namespace as second parameter, 'NAMESPACE', for instance. In that case you can access your templates by $app['twig']->render('#NAMESPACE/pathToATemplate'); Note #-char before namespace when you render it.

Something like this should work
$app['twig'] = $app->share($app->extend('twig', function($twig) use ($yourNewPath) {
$twig->addLoader(new \Twig_Loader_Filesystem($yourNewPath));
return $twig;
}));

Related

How can I reference a script that is in my pages directory?

I'm trying to keep a good practice of keeping context together and in that regard, I like how Razor Pages forces you to place your view models next to its view. I'd like to do this with script files as well.
For example, if script is a bit large and is only used on this particular page, it may not be the cleanest thing to have it in a render section in the cshtml. Instead, I'd want to place it into its own .js file. But to keep the project easy to navigate for team members, I'd like to place that .js file in the same folder next to its view and view model. I'm not sure how to link that file, or if I even can. If I give src a path to the file, it is not found. Assuming that's because wwwroot is the only folder setup to host static files.
Suggestions?
You can add an option to the StaticFilesProvider in your Configure method:
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),"Pages")), RequestPath="/Pages"
});
See the documentation on Static Files in ASP.NET Core: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.1

How to acess AreasController that is in controller folder that is outside of .Net's Areas Folder

I have a Controller called AreasController, I added Areas folder to the project so I can create some Areas, by doing this I noticed that the url: /Areas is pointing to Areas folder not to my AreasController.
Is there a way to solve this, or I'll have to rename AreasController.
You can specify the associated route to that controller by adding an annotation on the top of the class name
as here:
[Route("Areas")]
for more reading please check Microsoft docs

MVC 4 error in accessing resource file from controller

Getting error in accessing resource file string from MVC controller(working file with views)
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "TempNameSpace.App_GlobalResources.Resource_EN_US.resources" was correctly embedded or linked into assembly "TempNameSpace" at compile time, or that all the satellite assemblies required are loadable and fully signed.
What I have already done
1. Resource file build action : from content to Embedded resource
Result : Publish does not include resource files in it.
2. Added new resource file with name Resource.resx
Result : Same error
Solution hierarchy
1. Solution contains 4 projects, two with resource files(Resource.resx and Resource_EN_US under App_GlobalResources folder)
2. Designer for Resource_EN_US contains correct namespace
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TempNameSpace.App_GlobalResources.Resource_EN_US", typeof(Resource_EN_US).Assembly);
In my projects I use a seperate project for resources. I have set all resource files Build Action property to Embedded Resource.
On the reference to the resources project, I have Copy Local set to True for the resources project. (Rightclick a reference and select Properties)
If you have your resource files inside your web project, I think that the property corresponding with Copy Local should be Copy to Output Directory (Also accessible via rightclick and Properties).
Edit:
I see that in your comment you said that you access the file as Resource.Resource_EN_US.WaterMarkEmail but this is not the right way to access the files. You should access them as one single file: Resources.Property.
My files are named as: Standard.resx, Standard.en.resx, Standard.no.resx and so on. To access them I can use Standard.Property. The resource files will mold toghether, but only if you give them valid names. If you can access all your resource files individually, then you have not used the correct naming convention.
I recommend implementing a ResourceHelper that uses the ResourceManager to access the property that you need.

Using grailsLinkGenerator to generate link to images in bootstrap.groovy

I'm trying to create a link to an image resource in my bootsrap file using the underdocumented grailsLinkGenerator class in Grails 2.0.3
I have successfully injected the service using
...
LinkGenerator grailsLinkGenerator
...
But the url generated by the following code is not correct
grailsLinkGenerator.resource(dir:'images', file:'1.jpg')
There are no errorsm and the path generated almost looks correct, except it is missing 'static' in the url. Am I using this wrong? Is there an alternative way to access your static resources in bootstrap?
Looking at the ApplicationTagLib source, you can see that g:resource uses linkGenerator only when the resources plugin is not installed.
Closure resource = { attrs ->
if (!attrs.pluginContextPath && pageScope.pluginContextPath) {
attrs.pluginContextPath = pageScope.pluginContextPath
}
// Use resources plugin if present, but only if file is specified - resources require files
// But users often need to link to a folder just using dir
return ((hasResourceProcessor && attrs.file) ? r.resource(attrs) : linkGenerator.resource(attrs))
}
So this is probably the reason that your links don't have the static path.
I don't think that's a good option to store the full link to the thumbnail in the database. If you change something in the structure you will need to update all links. You can store the name of the thumbnail (or use a convention) and let the link be generated in a TagLib that will be responsible to show them.
In the TagLib you can just use g:resource that will create the correct url's.

Renaming namespace in ASP.NET MVC Project: compile OK, Running not OK

How to fix ?
Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'Home' has found the following matching controllers:
MyProject1.Controllers.HomeController
MyProject2.Controllers.HomeController
Probably need a little bit more details. For e.g. Did you change the namespace from MyProject1 to MyProject2?
Check your bin folder to see if any of dlls from the old namespace are still around. If that's the case cleaning them up and recompiling should fix the issue.
Make sure you edit the default namespace setting in your web project properties, on the Application tab.
I assume this is in your Views. Be certain the namespaces in your views is correct.

Resources