I want to pre-process the views before they are rendered by processing them and generating a new file, and then make it load the new file instead.
I've looked at Packages/Service providers or creating a custom View class which extends some class in Illuminate\View and hijack it with App::bind('view', 'CustomView'), but neither seems to work the way I want.
Is this possible somehow?
Example:
View::make('some_view');
Check if some_view has changed, if so pre-process and save the result in some cache dir, e.g. app/storage/cache/some_view.blade.php.
Make View load app/storage/cache/some_view.blade.php instead.
Try View::swap(new CustomView);
all facades has the swap() available. I have used it myself to swap the Hash function with my own class so I could use Laravel with an existing project.
Related
When using viewBinding with ConstraintLayout, maybe a lot of Ids will be create to help describe the view relationship, but will never used in Kotlin/Java.
I just found two useful tools attribute
tools:viewBindingIgnore="true" used to prevent whole layout generated in Binding, but not for single Id.
tools:viewBindingType="TextView" used for changing the Type generated in Binding.
So is there any way to ignore Id(s)? I don't want to expose them to pollute the Kotlin/Java.
Currently, there is no way to ignore view binding generation on view by view basis. If you use Proguard the unused ViewBinding IDs will be removed from the final APK.
On one MVC 4 project, we have a lot of Pages.cshtml that receive a collection of Models (generally hundreds of rows), which we serialize as JSON via
#Html.Raw(Json.Encode(Model));
The problem is that on some of those pages we are receiving an exception (The length of the string exceeds the value set on the maxJsonLength).
We know what is the reason and how to fix it. The thing is that I would like to create a similar Json.Encode() method (using Json.Net), so we do not need to modify all the cshtml pages. If I create a Json.Encode() method, Razor complains about ambiguous reference between My.Namespace.Json and System.Web.Helpers.Json.
Again, we know how to solve this by adding an alias on the page:
#using Json = My.Alias.Json
What I'm trying to do is to transparently instruct Razor to choose this alias for all the cshtml pages that uses this Json.Encode. The reason is that I want this to be transparent, so later if somebody adds a new page, automatically he will start to use our custom JSON implementation.
I think on Views/Web.config you can add namespaces and some configurations for Razor, but I don't see how to explicitly set the aliases.
Interesting issue, but as far as I know that's not possible. See e.g. this: C#: Globally alias a generic class name?
As an alternative you could create a helper method Html.JsonEncode() and teach, drill or entice everyone to use that.
If you make it also do the Raw() call and return IHtmlString, then you can do #Html.JsonEncode(Model) as opposed to #Html.Raw(Html.JsonEncode(Model)) and before you know it everybody is a fan of your new method.
I've got a widget base class and 50 different kinds of child widget subclasses. Each child widget provides its own partial view. A Page object is defined with a configuration of 2 to 3 of these widgets, and calls a view__generator method that creates a single view erb file for that particular page by retrieving and compositing the partial views that are associated with each of the widgets.
For example, a Page about dolphins might include an Image widget and a radio button choice widget. the generator function creates a single view page. (Yes, I know that it sounds like I should just be using built in rails and html constructs, but there is a method to the madness).
My problem is that I can't figure out how to composite the data requirements into a single set of code that can be run by the controller prior to rendering the composited view.
For example, widget A might require a query of Foo.all while widget B might require a calculation of a form variable called "total_sum".
I've considered two approaches:
1) figure out how to take widget-specific blocks of code and execute them in the context of the base Page controller.
I tried using Procs but the Proc variables aren't available outside of the scope of the Proc. I know that I can write the code into the partial views, but I'd really rather avoid the mixing of concerns.
2) do the data queries and computation on the widget model, and pass the data back as a hash that can then be bound to an instance variable in the base class and made available to the composited view.
I'm pretty sure 2 is the preferred direction, but I'd be interested in guidance on best practices.
I ended up doing what I describe in choice 2. Each model can pull up any data it needs and serialize it into a hash that is always made available to the view.
I'm quite new to Rails development and I came up with this question today. I have a method that returns some JSON data.
It is used to populate a select with a list of cities according to what was selected on a previous select (list of states). So, it's a simple method that loads a list based on some ajax parameter passed through and it is used all along my site.
I'm using Rails 4 and I placed this method on my HomeController. So everytime I need to fetch the list of cities, I call the HomeController to load the data.
Is this the correct approach or should I place this method on a more generic controller (like ApplicationController)? Is there a better way?
I think the best thing is to keep this modular. So you can create a separate controller for this, like StatesController - and possibly even a separate model if that makes sense for your application (I'm not sure where you're getting your data). There is no cost to having extra controllers, and this way your code is clean and organized, with each piece of functionality existing in its logical place.
Since Rails is using Ruby (dynamic language), would it be possible to create a very flexible
'configuration' class that has properties that you use throughout the website, AND have the ability to add new class properties (in the db for web modification) and then just use it in the code.
Each property would be of a specific type like a string, integer, bool etc.
You can't do this in a strongly typed language, but it must be possible with Ruby!
So say my class is like:
globalConfig.is_active
globalConfig.admin_email
I guess to make this work, I would loop through all the properties in the db, create properties in the class and assign the value right?
I actually have a settings plugin on GitHub you can use:
http://github.com/bellmyer/settings
It makes this easier for you. Right now it's not rails3-ready, so let me know if you need that. I also need to put in the time to roll it into a gem, instead of a plugin.
If you end up using it, let me know and I'll get it up to date. Otherwise, you can look at the code to see how I did things, and use that to help build your own custom solution.