NAME

Plack::Component::Tags::HTML - Plack component for Tags with HTML output.

SYNOPSIS

package App;

use base qw(Plack::Component::Tags::HTML);

sub _cleanup {
       my ($self, $env) = @_;
       # Cleanup about call().
       return;
}

sub _css {
       my ($self, $env) = @_;
       $self->{'css'}->put(
               # Structure defined by CSS::Struct
       );
       return;
}

sub _prepare_app {
       my $self = shift;
       # Preparation of app, before Plack::Component::call().
       return;
}

sub _process_actions {
       my ($self, $env) = @_;
       # Process actions in Plack::Component::call() before output.
       return;
}

sub _tags_middle {
       my ($self, $env) = @_;
       $self->{'tags'}->put(
               # Structure defined by Tags
       );
       return;
}

DESCRIPTION

This component is helper for creating Plack application with Tags. It is based on Plack::Component.

ACCESSOR METHODS

author

Author string to HTML head. Default value is undef.

content_type

Content type for output. Default value is 'text/html; charset=__ENCODING__'.

css

CSS::Struct::Output object. Default value is CSS::Struct::Output::Raw->new.

css_init

Reference to array with CSS::Struct structure. Default value is CSS initialization from Tags::HTML::Page::Begin like

 * {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
 }

encoding

Set encoding for output. Default value is 'utf-8'.

favicon

Link to favicon. Default value is undef.

flag_begin

Flag that means begin of html writing via Tags::HTML::Page::Begin. Example is in EXAMPLE2. Default value is 1.

flag_end

Flag that means end of html writing via Tags::HTML::Page::End. Example is in EXAMPLE2. Default value is 1.

generator

Generator string to HTML head. Default value is undef.

html_lang

HTML element language. Default value is 'en'.

psgi_app

PSGI application to run instead of normal process. Intent of this is change application in _process_actions method. Default value is undef.

script_js

Reference to array with Javascript code strings. Default value is [].

script_js_src

Reference to array with Javascript URLs. Default value is [].

status_code

HTTP status code. Default value is 200.

tags

Tags::Output object. Default value is

Tags::Output::Raw->new(
        'xml' => 1,
        'no_simple' => ['script', 'textarea'],
        'preserved' => ['pre', 'style'],
);

title

Title of page. Default value is undef.

METHODS TO OVERWRITE

_cleanup

Method to cleanup after call(). Arguments are $self and $env.

_css

Method to set css via $self->{'css'} object. Arguments are $self and $env.

_prepare_app

Method to set app preparation part. Called only once on start. Argument is $self only.

There are defaults for:

css()
encoding()
flag_begin()
flag_end()
content_type()
script_js()
script_js_src()
status_code()
tags()

When you are rewriting _prepare_app() and want to use this defaults must place $self-SUPER::_prepare_app()> to this.

_process_actions

Method to set app processing part. Called in each call before creating of output. Arguments are $self and $env.

_tags_middle

Method to set tags via $self->{'tags'} object. Arguments are $self and $env.

METHODS IMPLEMENTED

call

Inherited from Plack::Component. There is run of:

$self->_process_actions($env);
$self->_css($env);
$self->_tags($env);
$self->_cleanup($env);

After it Generate and encode output from Tags to output with HTTP code. HTTP status code is defined by status_code() method and Content-Type is defined by content_type method.

prepare_app

Call _prepare_app().

ERRORS

prepare_app():
        Accessor 'css' must be a 'CSS::Struct::Output' object.
        Accessor 'tags' must be a 'Tags::Output' object.

EXAMPLE1

package App;

use base qw(Plack::Component::Tags::HTML);
use strict;
use warnings;

sub _tags_middle {
        my ($self, $env) = @_;

        $self->{'tags'}->put(
                ['d', 'Hello world'],
        );

        return;
}

package main;

use Plack::Runner;

my $app = App->new(
        'title' => 'My app',
)->to_app;
my $runner = Plack::Runner->new;
$runner->run($app);

# Output:
# HTTP::Server::PSGI: Accepting connections at http://0:5000/

# Output by HEAD to http://localhost:5000/:
# 200 OK
# Date: Sun, 31 Oct 2021 10:35:33 GMT
# Server: HTTP::Server::PSGI
# Content-Length: 166
# Content-Type: text/html; charset=utf-8
# Client-Date: Sun, 31 Oct 2021 10:35:33 GMT
# Client-Peer: 127.0.0.1:5000
# Client-Response-Num: 1

# Output by GET to http://localhost:5000/:
# <!DOCTYPE html>
# <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>My app</title><style type="text/css">
# *{box-sizing:border-box;margin:0;padding:0;}
# </style></head><body>Hello world</body></html>

EXAMPLE2

package App;

use base qw(Plack::Component::Tags::HTML);
use strict;
use warnings;

sub _tags_middle {
        my ($self, $env) = @_;

        $self->{'tags'}->put(
                ['d', 'Hello world'],
        );

        return;
}

package main;

use Plack::Runner;

my $app = App->new(
        'flag_begin' => 0,
        'flag_end' => 0,
        'title' => 'My app',
)->to_app;
my $runner = Plack::Runner->new;
$runner->run($app);

# HTTP::Server::PSGI: Accepting connections at http://0:5000/

# Output by HEAD to http://localhost:5000/:
# 200 OK
# Date: Sun, 27 Feb 2022 18:52:59 GMT
# Server: HTTP::Server::PSGI
# Content-Length: 11
# Content-Type: text/html; charset=utf-8
# Client-Date: Sun, 27 Feb 2022 18:52:59 GMT
# Client-Peer: 127.0.0.1:5000
# Client-Response-Num: 1

# Output by GET to http://localhost:5000/:
# Hello world

DEPENDENCIES

CSS::Struct::Output::Raw, Encode, Error::Pure, Plack::Component, Plack::Util::Accessor, Scalar::Util, Tags::HTML::Page::Begin, Tags::HTML::Page::End, Tags::Output::Raw.

SEE ALSO

Plack::Component

Base class for PSGI endpoints

REPOSITORY

https://github.com/michal-josef-spacek/Plack-Component-Tags-HTML

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2020-2024 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.18