Code Monkey home page Code Monkey logo

tusk's Introduction

Plugin code replacement in a string for Laravel

Problem

Your user needs to inject a chunk of code into a basic string using their CMS functionality. You want them to be able to set a Google map or text from a database but you don't want them to be able to run code.

Solution

Set up their code and allow them to reference it in their text string.

Installing

Add the dependency to your project:

composer require DNABeast/tusk

Laravel 5.2:

After updating composer, add the ServiceProvider to the providers array in config/app.php

DNABeast\Tusk\TuskServiceProvider::class,

Add the directory Plugin/lib to your App directory to contain your plugin files.

You may need to clear the view cache

php artisan view:clear

Usage

In your blade file put your string inside the tusk directive.

@tusk('Your string [[- map -]]. Rest of String')

This writes a file in App\Plugins called map.php. If this file already exists nothing happens.

If your map.php file was this

<?php echo '<iframe>Map</iframe>'; ?>

The blade file would replace [[- map -]] and output

Your string <iframe>Map</iframe>. Rest of String

Use Case

  1. Your user can update their contact page with basic text but you don't want to allow iframes. You can still allow them to enter [[- googlemap -]] to inject the google map html required to embed a map.

  2. You have code that can spit out a list of links from the database. You also have basic pages on the site that allow the user to update their content and they can include the list code without having access to the actual code by simply entering [[- linklist -]].

Potential pitfall

You plugin file can be all html but if it has php you need to echo the result. If you return it nothing will display. ie.

<?php
	$x = 34+65;
	return $x; // <- This wont work. You must echo it.
?>

tusk's People

Contributors

dnabeast avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.