Skip to content

Instantly share code, notes, and snippets.

@ybelenko
Last active October 28, 2023 11:21
Show Gist options
  • Save ybelenko/3b0b9d99404393dbd9bf1a474726c0b4 to your computer and use it in GitHub Desktop.
Save ybelenko/3b0b9d99404393dbd9bf1a474726c0b4 to your computer and use it in GitHub Desktop.
{
// Place your snippets for html here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
"slim4 skeleton Twig template": {
"prefix": "extends",
"body": [
"{% extends \"layout/${1|layout-empty,layout|}.twig\" %}",
"",
"{% block css %}",
" {% webpack_entry_css '${TM_DIRECTORY/^.+\\/(.*)$/$1/}/$TM_FILENAME_BASE' %}",
"{% endblock %}",
"",
"{% block js %}",
" {% webpack_entry_js '${TM_DIRECTORY/^.+\\/(.*)$/$1/}/$TM_FILENAME_BASE' %}",
"{% endblock %}",
"",
"{% block content %}",
"",
" <div class=\"container\">",
" $0",
" </div>",
"",
"{% endblock %}",
""
],
"description": "slim4 skeleton Twig template"
}
}
{
// Place your snippets for php here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
"slim4 skeleton route": {
"prefix": [
"$app->get",
"$group->get"
],
"body": [
"// Route for /$1",
"\\$${2|app,group|}->${3|get,post,put,patch,delete|}('/$1', \\App\\Action\\\\$4::class)->setName('${1/[^A-z^0-9^-]/:downcase/g}');"
],
"description": "Route declaration for Slim4 skeleton"
},
"slim4 skeleton action class": {
"prefix": "class",
"body": [
"namespace App\\Action\\\\${TM_DIRECTORY/^.+\\/(.*)$/$1/};",
"",
"use App\\Responder\\Responder;",
"use Psr\\Http\\Message\\ResponseInterface;",
"use Psr\\Http\\Message\\ServerRequestInterface;",
"",
"/**",
" * Action.",
" */",
"final class $TM_FILENAME_BASE",
"{",
" /**",
" * @var Responder",
" */",
" private \\$responder;",
"",
" /**",
" * The constructor.",
" *",
" * @param Responder \\$responder The responder",
" */",
" public function __construct(Responder \\$responder)",
" {",
" \\$this->responder = \\$responder;",
" }",
"",
" /**",
" * Action.",
" *",
" * @param ServerRequestInterface \\$request The request",
" * @param ResponseInterface \\$response The response",
" *",
" * @return ResponseInterface The response",
" */",
" public function __invoke(ServerRequestInterface \\$request, ResponseInterface \\$response): ResponseInterface",
" {",
" return \\$this->responder->withTemplate(\\$response, '${TM_DIRECTORY/^.+\\/(.*)$/${1:/downcase}/}/$0.twig');",
" }",
"}",
""
],
"description": "Slim4 skeleton action class"
}
}
@ybelenko
Copy link
Author

VSCode Snippets for odan/slim4-skeleton

If you're not familiar with VSCode snippets here is official doc:
Snippets in Visual Studio Code - Create your own snippets

slim4 skeleton Twig template

How to use

  1. Create file templates/home/hello-world.twig
  2. Start typing extends
  3. Pick slim4 skeleton Twig template from autocomplete dropdown

Generated file should look like:

{% extends "layout/layout-empty.twig" %}

{% block css %}
    {% webpack_entry_css 'home/hello-world' %}
{% endblock %}

{% block js %}
    {% webpack_entry_js 'home/hello-world' %}
{% endblock %}

{% block content %}

    <div class="container">
        
    </div>

{% endblock %}

slim4 skeleton route

How to use

  1. Start typing $app or $group in config/routes.php
  2. Pick slim4 skeleton route from autocomplete dropdown

Generated code should look like:

// Route for /
$app->get('/', \App\Action\::class)->setName('');

slim4 skeleton action class

How to use

  1. Create file src/Action/Home/HelloWordAction.php
  2. Write first <?php and add empty line so VSCode can understand that we want to call PHP related snippet
  3. Start typing class
  4. Pick slim4 skeleton action class from autocomplete dropdown

Generated file should look like:

<?php

namespace App\Action\Home;

use App\Responder\Responder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
 * Action.
 */
final class HelloWorldAction
{
    /**
     * @var Responder
     */
    private $responder;

    /**
     * The constructor.
     *
     * @param Responder $responder The responder
     */
    public function __construct(Responder $responder)
    {
        $this->responder = $responder;
    }

    /**
     * Action.
     *
     * @param ServerRequestInterface $request The request
     * @param ResponseInterface $response The response
     *
     * @return ResponseInterface The response
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        return $this->responder->withTemplate($response, 'home/.twig');
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment