Chess diagram embedding into the “first application” “nette-blog”, into the blog posts ($post->content)

Notice: This thread is very old.
j8i
Member | 2
+
0
-

Good day,
I would like to add a chess diagram embedding into the “first application” “nette-blog”,
into the blog posts ($post->content).

e.g. including a “macro”:

<pre>
{{Chess diagram| tright
|
|=
8 |rd|nd|bd|qd|kd|bd|nd|rd|=
7 |pd|pd|pd|pd|pd|pd|pd|pd|=
6 |  |  |  |  |  |  |  |  |=
5 |  |  |  |  |  |  |  |  |=
4 |  |  |  |  |  |  |  |  |=
3 |  |  |  |  |  |  |  |  |=
2 |pl|pl|pl|pl|pl|pl|pl|pl|=
1 |rl|nl|bl|ql|kl|bl|nl|rl|=
   a  b  c  d  e  f  g  h
|
}}
</pre>

or somehow.

I am nette-beginner and I would like to take an advice, “path” how it can be probably implemented. (by filters or by componets or by macros rendered from strings)

CZechBoY
Member | 3608
+
0
-

How common is the usage of writing chess diagram? I think once maybe twice on one page, so you should use component for that.
I use template filters for little modifing text/number or to localize text/number. Most of them outputs only one line of text.

David Matějka
Moderator | 6445
+
0
-

Hi, check this example, it's a bit similar to your chess diagram.

Slava.Aurim
Member | 19
+
0
-

If you need only render html, and do not need to handle some signals from chess diagram (via links or ajax or POST requests), IMHO, it may be implemented via custom Latte macros:

{chess_diagram "
8 |rd|nd|bd|qd|kd|bd|nd|rd|
7 |pd|pd|pd|pd|pd|pd|pd|pd|
6
5
4
3
2 |pl|pl|pl|pl|pl|pl|pl|pl|
1 |rl|nl|bl|ql|kl|bl|nl|rl|
"}

IMHO, so it may be easier than component. Right?

Last edited by Slava.Aurim (2016-09-14 18:10)

CZechBoY
Member | 3608
+
0
-

@Slava.Aurim How should you use it?

j8i
Member | 2
+
0
-

Slava.Aurim wrote:

If you need only render html, and do not need to handle some signals from chess diagram (via links or ajax or POST requests), IMHO, it may be implemented via custom Latte macros:

{chess_diagram "
8 |rd|nd|bd|qd|kd|bd|nd|rd|
7 |pd|pd|pd|pd|pd|pd|pd|pd|
6
5
4
3
2 |pl|pl|pl|pl|pl|pl|pl|pl|
1 |rl|nl|bl|ql|kl|bl|nl|rl|
"}

IMHO, so it may be easier than component. Right?

I believe that custom Latte macro must be sufficient in template for chess diagram, but I don't see how to make it working in $post->content data?

Slava.Aurim
Member | 19
+
0
-

It depends, where you create or can get Latte object.

Below my draft example with standalone Latte usage.

#file: app.php

$latte = new Latte\Engine;
YouClassWithMacros:install($latte->getCompiler());

// ... assign some $params for template
$params = ['paramName'=>'paramValue', /* ... */ ];

$latte->render('path/to/you/template.latte', $params);

and describe macros in separate class:
(similarly as in class Latte\Macros\BlockMacros, for example)

#file: YouClassWithMacros.php

class YouClassWithMacros extends MacroSet
{
	public static function install(Latte\Compiler $compiler)
	{
		$me = new static($compiler);
		$me->addMacro('chess_diagram', [$me, 'macroChessDiagram']);
	}

	// compile macro to php-code
	public function macroChessDiagram(MacroNode $node, PhpWriter $writer)
	{

		return $writer->write("echo %modify(". __CLASS__."::renderChessDiagram(%node.args));")
	}

	// parse $diagramMarkup to html-code
	static function renderChessDiagram($diagramMarkup)
	{
		// ... you logic here
		return $html;
	}

}

I'm not sure what is the easiest way. Alternatively, you can also use a standart “include” macro ({include 'chess-diagramm.latte', markup => "...markup..."}) instead of calling the own macro. And in included latte partial eou can simply parse markup ({php ... }) and immediately render the chess diagram.

See also: https://latte.nette.org/#…

Slava.Aurim
Member | 19
+
0
-

I believe that custom Latte macro must be sufficient in template for chess diagram, but I don't see how to make it working in $post->content data?

May be compile $post->content as latte code? (with StringLoader) (if only you is author of blog)

Last edited by Slava.Aurim (2016-09-14 22:24)

CZechBoY
Member | 3608
+
0
-

@Slava.Aurim So there is another problem – where you get chess markup in format which your macro understand :-)

Slava.Aurim
Member | 19
+
0
-

I didn't understand, what the problem is? I guess topicstarter know where from.