PHP code example of lucasdavies / wikitextbuilder

1. Go to this page and download the library: Download lucasdavies/wikitextbuilder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lucasdavies / wikitextbuilder example snippets


use LucasDavies\WikitextBuilder\Builder;

$builder = new Builder();

$builder->heading('Section Title', 2); // ==Section Title==
$builder->heading('Subsection', 3);    // ===Subsection===

// Basic template
$builder->template('Citation needed');
// {{Citation needed}}

// Template with unnamed parameters
$builder->template('Convert')->params(['100', 'km', 'mi']);
// {{Convert|100|km|mi}}

// Template with named parameters
$builder->template('Infobox')
    ->params([
        'name' => 'Example',
        'type' => 'Sample'
    ]);
// {{Infobox|name=Example|type=Sample}}

// Multiline template
$builder->template('Infobox')
    ->multiline()
    ->params([
        'name' => 'Example',
        'type' => 'Sample'
    ]);
// {{Infobox
// |name=Example
// |type=Sample
// }}

// Multiline with spacing
$builder->template('Infobox')
    ->multiline()
    ->spaced()
    ->params([
        'name' => 'Example',
        'type' => 'Sample'
    ]);
// {{Infobox
// |name = Example
// |type = Sample
// }}

// Multiline with alignment
$builder->template('Infobox')
    ->multiline()
    ->aligned()
    ->params([
        'name' => 'Example',
        'description' => 'A longer parameter'
    ]);
// {{Infobox
// |name        = Example
// |description = A longer parameter
// }}

use LucasDavies\WikitextBuilder\Components\Template;

$template = new Template('Citation needed');
echo $template->render();
// {{Citation needed}}

$builder->table(function (Table $table) {
    return $table
        ->caption('Example Table')
        ->headerRow(['Column 1', 'Column 2', 'Column 3'])
        ->bodyRow(['Value 1', 'Value 2', 'Value 3'])
        ->bodyRow(['Value 4', 'Value 5', 'Value 6']);
});

$builder->table(function (Table $table) {
    return $table
        ->styles(['width' => '100%', 'margin' => 'auto'])
        ->classes(['sortable', 'collapsible'])
        ->headerRow(['Name', 'Age', 'City'])
        ->bodyRow(['Alice', '30', 'New York']);
});

use LucasDavies\WikitextBuilder\Components\Table;

echo new Table()
    ->styles(['width' => '100%', 'margin' => 'auto'])
    ->classes(['sortable', 'collapsible'])
    ->headerRow(new TableHeaderRow(['Name', 'Age', 'City']))
    ->bodyRow(new TableBodyRow(['Alice', '30', 'New York']))
    ->render();

use LucasDavies\WikitextBuilder\Components\Table\TableCell;

$cell = new TableCell('value')->textCenter();
echo $cell->render();
// |style="text-align:center"|value

use LucasDavies\WikitextBuilder\Components\Table\TableCell;

$cell = new TableCell('value')->textCenter()->styles(['color' => 'red']);
echo $cell->render();
// |style="text-align:center;color:red"|value

use LucasDavies\WikitextBuilder\Components\Table\TableCell;

// Custom style applied after predefined style
$cell = new TableCell('value')->textCenter()->styles(['color' => 'red', 'text-align' => 'left']);
echo $cell->render();
// |style="color:red;text-align:left"|value

// Predefined style applied after custom style
$cell = new TableCell('value')->styles(['text-align' => 'left', 'color' => 'green'])->textRight();
echo $cell->render();
// |style="color:red;text-align:right"|value