Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcode support #98

Closed
wants to merge 1 commit into from
Closed

Shortcode support #98

wants to merge 1 commit into from

Conversation

Fl0ux
Copy link

@Fl0ux Fl0ux commented Sep 5, 2016

I finally found some time to propose you a more complete version of my shortcode support.

This middleware allows to configure shortcodes using a config file. A shortcode like "[exemple]" will call a function and replace it by a view.
This can be useful to allow users to easily insert advanced functionality when they are creating page on their website for example.

Configuration example :

return [
// This [contact] shortcode will be replaced by a view
'contact' => [
'view' => 'contact'
],
// This one will call Modules\Boats\Entities\Boat::prepareResult and display it's result in the "boat.result-content" view
'boats' => [
'view' => 'boat.result-content',
'callback' => [Modules\Boats\Entities\Boat::class, 'prepareResult']
],
'last-minute' => [
'view' => 'boat.result-content',
'callback' => [Modules\Boats\Entities\Boat::class, 'prepareOffers']
],
// In this example, a rendering function is called, so no view is needed
'sitemap' => [
'callback' => 'generateSitemap'
]
];

To be enabled, "asgard.page.config.php" needs to configured that way :

'middleware' => [ 'shortcode' => \Modules\Core\Http\Middleware\ShortcodeMiddleware::class ]

This middleware allows to configure shortcodes using a config file. A shortcode like "[exemple]" will call a function and replace it by a view.

Configuration example :

return [
    // This [contact] shortcode will be replaced by a view
    'contact' =>  [
        'view' => 'contact'
    ],
    // This one will call Modules\Boats\Entities\Boat::prepareResult and display it's result in the "boat.result-content" view
    'boats' => [
        'view' => 'boat.result-content',
        'callback' => [Modules\Boats\Entities\Boat::class, 'prepareResult']
    ],
    'last-minute' => [
        'view' => 'boat.result-content',
        'callback' => [Modules\Boats\Entities\Boat::class, 'prepareOffers']
    ],
    // In this example, a rendering function is called, so no view is needed
    'sitemap' => [
        'callback' => 'generateSitemap'
    ]
];
// Get defined shortcodes from the config file
$shotcodesDefinitions = config('asgard.core.shortcodes', []);

foreach ($shotcodesDefinitions as $shortcodeName => $options) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 1 newline after opening brace; 2 found

@nWidart
Copy link
Member

nWidart commented Sep 5, 2016

Hello,

Thanks for your contribution and time spent!

Besides the code style issues I have a few comments.
This function is way to big, it should be abstracted to a/multiple classes.

  • It can't be tested
  • To many indentations (cyclic complexity)

Extracting this to a class(es) will allow independent tests and reduce all those conditions to one level only.

@mikemand
Copy link

mikemand commented Sep 6, 2016

It would probably be better to implement an already established Shortcode parser, than try to create our own.

This one looks like it could make use of the config system you already thought up (for example, your middleware could add each configured shortcode as a handler).

I can see several uses for shortcodes in the Page module alone, so I hope we can get something implemented.

@nWidart nWidart closed this Jun 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants