1 year ago

Dynamically load views that corresponds to database classes

Posted 1 year ago by oliverbusk

I have created a rule-based engine, that can transform text in a couple of different ways, that my users decide. Consider below string of text:

Raw string Now as explained, I wish to allow my users to perform/apply a range of different methods to the above text. They should be able to apply all rules, in any given order. Consider below example where I have performed 4 rules to the original text document:


Below is the database content of above rules:

1 | 5 | remove_empty_lines | null
2 | 5 | text_replace | a:2:{s:6:"search";s:9:"Laracasts";s:7:"replace";s:6:"Google";}
3 | 5 | regex_text_replace | a:2:{s:7:"pattern";s:9:"/Google/i";s:11:"replacement";s:6:"Amazon";}
4 | 5 | start_position_no_lines | a:1:{s:4:"Line";s:1:"2";}

Now the images above is hardcoded, as I am having troubles on how to display the rules dynamically.

For example, for rule #2 ("Replace Text"), I have two arguments:


Which can look like this in my blade view:

@foreach ($rules as $rule)
<div class="w-1/3 flex items-center">{{ucwords(str_replace('_', ' ', $rule->method))}}</div>
<input type="text" value="{{$rule->arguments['search']}}" placeholder="Search for...">
<input type="text" value="{{$rule->arguments['replace']}}" placeholder="Replace with...">

Now, the problem arises when a new rule is to be shown. For example rule #4 ("Choose Start Position"). For this rule, it takes two arguments as well.


But this does not match the code I have in my .blade.php file. This rule does not have a search or replace argument.

What to do?

I am not quite sure on how to proceed here. I was thinking that I might be able to extend blade views dynamically. Something like:

foreach($rules as $rule)
@include('layouts.rules.' . $rule->method)

<!-- for example: views/layouts/rules/text_replace-blade.php -->
<!-- for example: views/layouts/rules/regex_text_replace-blade.php -->
<!-- for example: views/layouts/rules/start_position_no_lines-blade.php -->


Not sure if this approach makes sense though?

Have anyone tried something like this - or maybe just have some comments?

Please sign in or create an account to participate in this conversation.